[英]How can I do a cyclic shift for a x y plot in matlab?
I have a number of CSV files, each have two columns, like the following: 我有许多CSV文件,每个文件都有两列,如下所示:
X Y
-93 4.3249598727193514e-5
-92 0.0017247823556143683
-91 0.009753849011964329
-90 0.03288075738113382
.....
96 0.004679186277368824
97 0.0003355482914528248
98 3.1077568577666192e-6
I plot all the CSV files in a directory using the following code: 我使用以下代码在目录中绘制所有CSV文件:
function myplot(x,y, marker)
plot(x(:,1),x(:,2)); hold on;
end
dd = '.';
dx = 0;
all_in_one = 1;
files = getAllFiles(dd,'*.csv',1); % dir(strcat(dd,'*.csv_'))
Legend = cell([size(files),1]);
k =1;
set(0,'DefaultFigureWindowStyle','docked')
s = pwd;
h = figure('Name',s(20:end));
domag = 0;
for file = files'
data = load(file{1});
if all_in_one == 0
figure('Name',file{1});
end
if dx == 1
[C, ~, ic]= unique(data(:,3));
A = [C accumarray(ic,data(:,4),[],@mean)];
myplot(A); hold on;
else
myplot(data); hold on;
end
avg = num2str(round(avg,1));
Legend{k}= strcat(file{1}, '::', avg);
k = k+1;
end
if all_in_one == 1
legend(Legend);
f2 = strrep(s(20:end),'\','_');
f2 =strcat(f2,'.fig');
saveas(h,f2,'fig');
%saveas(h,f2,'png');
end
And I have the following figure: 我有下图:
As you can see, it plots some curves between about -100 and 100. Now I want to shift the plots about 50 to the right. 如您所见,它在-100到100之间绘制了一些曲线。现在,我想将图向右移动约50。 However, when I shift it the numbers which exceed the boundaries must appear back from the left of the image, so that the current intersections on 0 and 90 must be on 50 and -50. 但是,当我移动它时,超出边界的数字必须从图像的左侧出现,因此当前在0和90处的交点必须在50和-50处。
I can shift the x axis using the following command in the loop. 我可以在循环中使用以下命令移动x轴。 However, I have not much idea how to translate the whole data! 但是,我不太了解如何转换整个数据!
shift = -50;
data(:,1) = data(:,1)+shift;
This code moves in a cyclic manner values exceeding a lower bound and upper bound 这段代码以循环方式移动,其值超过下限和上限
lb = -100; % Lower bound
ub = 100; % Upper bound
% Indices less than lower bound
less_than_lb = find(data(:,1) < lb);
% Indices greater than upper bound
greater_than_ub = find(data(:,1) > ub);
% Cyclic shift values exceeding lower bound
data(less_than_lb,1) = ub - (lb - data(less_than_lb,1));
% Cyclic shift values exceeding upper bound
data(greater_than_ub,1) = lb - (ub - data(greater_than_ub,1));
% Sort shifted data
[data(:,1), ind] = sort(data(:,1));
data(:,2) = data(ind,2);
One solution using circshift
: 一种使用circshift
解决方案:
%dummy data
x = -90:10:90;
y = normpdf(x,0,100);
bnd = [-100,100];
shift = 50;
%shift x
xs = x+shift;
%which value are below/above boundary
ts = xs <= bnd(1) | xs >= bnd(2)
%shift direction
ss = sign(shift);
%x value correction
xs(ts) = xs(ts)-ss*sum(abs(bnd))
%create new vector
[x,ind] = sort(circshift(xs,ss*(sum(ts))))
y = y(ind);
%avoid to get a line between the first and last value by using a NaN
seiz = find(diff(ind)<0);
if ~isempty(seiz)
x = [x(1:seiz),NaN,x(seiz+1:end)];
y = [y(1:seiz),NaN,y(seiz+1:end)];
end
% plot the result:
plot(x,y)
Initial data : 初始数据 :
Shifted data : 移位数据 :
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.