簡體   English   中英

如何在Matlab中對軸圖進行循環移位?

[英]How can I do a cyclic shift for a x y plot in matlab?

我有許多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 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);

一種使用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)

初始數據

在此處輸入圖片說明

移位數據

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM