简体   繁体   中英

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:

 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:

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. 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.

I can shift the x axis using the following command in the loop. 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 :

%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 :

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM