Plot several graphs with differnt sample time in one figure
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
There is several data that I want to plot in one figure, but they are with different sample times, as some are gotten from measurements, while others are gotten from simulations. Also, some of the relative values may be very different, so I need to show them in different axises, for now I choose plotyy .
When they are with the same sample time, the code works well, as follows,
x = 0:1:10; % Sample Time is 1000ms
y1 = sin(x); % y1~y5 with the same time
y2 = cos(x);
y3 = 10*sin(x/2);
y4 = 10*cos(x/2);
y5 = x;
%-------------------------
figure
[handle_DualAxes,handle_line1,handle_line2] = plotyy(x,[y1;y2],x,[y3;y4;y5]);
But when I use this method with different sample times, like
x1 = 0:0.01:10; % Sample Time1 = 10ms
x2 = 0:1:10;% Sample Time2 = 1000ms
y1 = sin(x1);%With sample time1
y2 = sin(x2);%With sample time12
y3 = 10*sin(x1/2);%With sample time1
y4 = 10*sin(x2/2);%With sample time2
y5 = x2;%With sample time2
%-------------------------
figure
[handle_DualAxes,handle_line1,handle_line2] = plotyy([x1;x2],[y1;y2],[x1;x2;x2],[y3;y4;y5]);
Error message accurs,
??? Error using ==> vertcat
CAT arguments dimensions are not consistent.
Error in ==> Dual_Axes_Try at 47
[handle_DualAxes,handle_line1,handle_line2] =
plotyy([x1;x2],[y1;y2],[x1;x1;x2],[y3;y4;y5]);
How should I use the plotyy here? Or if there is any other way to draw this figure. Thanks.
0 Commenti
Risposta accettata
dpb
il 29 Gen 2015
...plotyy([x1;x2],[y1;y2],...
The problem here has nothing to do with plotyy, per se, but in Matlab can only have "jagged" arrays as cell arrays and, as you know, your two sets of variables aren't the same length/size...
Solution is one of two ways...
a) plot one set, and then add the second with the hold status 'on'...
[hAx,hL1,hL2] = plotyy(x1,y1,x1,y3); % First x vector
hold(hAx(1),'all'), hold(hAx(2),'all') % For some reason, hold won't accept array
plot(hAx(1),x2,y2)
plot(hAx(2),x2,[y4 y5])
I think I kept straight which you want on which axes...
Alternative b) is to augment the shorter arrays to same length as longer with NaN which plot and friends will ignore...
nanvec=nan(1,length(x1)-length(x2));
xn=[x2 nanvec]].' % longer vector of same length x1
yNL=[y1; [y2 nanvec]].'; % left axes y vector augmented
yNR=[y3; [y4 nanvec]; [y5 nanvec]].'; % right axes y vector augmented
hAx=plotyy(xn,yNL,xn,yNR); % plot 'em all at once
Or, another route that for plotting may be sufficient is to simply only plot every Nth point of the longer to make the same number as the shorter...depending on the data it may be sufficient for visualization.
Or, use interp1 or another interpolation routine to sample the higher at the same locations as the other. Any number of choices when get more exotic.
3 Commenti
dpb
il 30 Gen 2015
Which orientation for x and y vectors? I presumed were row as that is default for the output from the colon operation. Example--
Given
x1 = 0:0.01:10; % Sample Time1 = 10ms
x2 = 0:1:10;% Sample Time2 = 1000ms
y1 = sin(x1);%With sample time1
y2 = sin(x2);%W
>> xn=nan(1,length(x1)-length(x2));
>> Y=[y1;[y2 xn]];
>> whos Y
Name Size Bytes Class Attributes
Y 2x1001 16016 double
>>
This is before the transpose into a column-oriented array that plot expects for multiple series.
Moral: Make sure you've got the orientations correct.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Two y-axis in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!