如何在matlab中对XY图进行循环移位?
我有许多CSV文件,每个文件有两列,如下所示:如何在matlab中对XY图进行循环移位?,matlab,csv,plot,translation,Matlab,Csv,Plot,Translation,我有许多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 我使用以下代码绘制目录中的所有CSV文件: function
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
我使用以下代码绘制目录中的所有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
我有以下数字:
如您所见,它绘制了一些介于-100和100之间的曲线。现在我想把图向右移动50左右。但是,当我移动它时,超出边界的数字必须从图像的左侧显示回来,因此0和90上的当前交点必须在50和-50上
我可以在循环中使用以下命令移动x轴。然而,我不知道如何翻译整个数据
shift = -50;
data(:,1) = data(:,1)+shift;
此代码以循环的方式移动超过下限和上限的值
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);
lb=-100;%下限
ub=100;%上限
%小于下限的指数
小于磅=查找(数据(:,1)<磅);
%大于上限的索引
大于等于查找(数据(:,1)>ub);
%循环移位值超过下限
数据(小于磅,1)=ub-(磅-数据(小于磅,1));
%循环移位值超过上限
数据(大于1)=lb-(ub-数据(大于1));
%排序移位数据
[data(:,1),ind]=排序(data(:,1));
数据(:,2)=数据(ind,2);
使用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)
%虚拟数据
x=-90:10:90;
y=normpdf(x,0100);
bnd=[-100100];
班次=50;
%移位x
xs=x+shift;
%哪个值低于/高于边界
ts=xs=bnd(2)
%转向
ss=符号(移位);
%x值校正
xs(ts)=xs(ts)-ss*sum(abs(bnd))
%创建新向量
[x,ind]=排序(循环移位(xs,ss*(总和(ts)))
y=y(ind);
%避免使用NaN在第一个值和最后一个值之间获得一行
捕捉=查找(差异(ind)你的意思是循环移位,从100
超过x
的量将移动到-100+x
。。。为了使问题更容易阅读,如果你能把问题简化为一个最小的例子,那就太好了,因为90%的内容都是无关的。谢谢,它在每条曲线的两端画了一条直线,你有解决办法吗?@Ahmad:进行排序。第1行:[data(:,1),ind]=sort(data(:,1));
第2行:data(:,2)=数据(ind,2);