Save in fig file only the data within the limits of the axes

7 views (last 30 days)
I am plotting large vectors to a figure. After setting the x and y limits, the display data becomes a fraction of the initial vectors. In order to chose the limits, I need to see the full spectrum of the data. When saving I would like to reduce the size of the output file (.fig) to minimum. Therefore I am asking if figures could be save to .fig, containing only the data within the limits of the figure and not the entire initial ploted vectors (resulting in a smaller file). this is an automatic action so I am referring to matlab code for the saving function.

Answers (1)

dpb
dpb on 16 Mar 2015
Edited: dpb on 17 Mar 2015
Think you have to write a function to do it from the chosen final limits...not too difficult to do.
xl=xlim; yl=ylim; % Retrieve the current plot limits
x=x(iswithin(x,xl(1),xl(2)); % select the values within those limits
y=y(iswithin(y,yl(1),yl(2));
set(hL,'xData',x,'yData',y) % set plot data to the subset above
In the above I have assumed --
x,y are the data you've plotted; your vector variables go here of course
hL is the handle to the line on the plot -- save it with the *plot* call
iswithin is my utility function--
>> type iswithin
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
ERRATUM
Above doesn't select the correlated points; my bad...
xl=xlim; yl=ylim; % Retrieve the current plot limits
idx=iswithin(x,xl(1),xl(2)); % x values within logical address vector
x=x(ix); y=y(ix); % select those that are within on x-direction
idx=iswithin(y,yl(1),yl(2)); % now those inside on the y axes, too...
x=x(ix); y=y(ix); % now have those that are inside both only
set(hL,'xData',x,'yData',y) % set plot data to the subset above
NB: idx is an addressing logical array used for each direction in turn.
  4 Comments
Cristian JECU
Cristian JECU on 18 Mar 2015
The inconvenience with your code was that you tested each value to check if is within the limits while for my use my X is a time (linear growing function) so there was no need to check more than redefining the xData and yData corresponding to the limits of X. I agree that for an all purpose figure only your solution should work. But given my monotonically growing X that is very large, my proposition is faster.
Thanks again for your help!
dpb
dpb on 18 Mar 2015
Edited: dpb on 18 Mar 2015
No problem...I've not timed it and I did look at the loop too quickly; I thought you were doing a find over the data array initially but it's the handles array...
If the data are monotonic then using
... temp(find(Xdata{1}==xl(1),1):find(Xdata{1}==xl(2),1,'last'));
should help at least a little, particularly if the arrays are sizable.
Also, should be able to take the search for X positions out of the loop; the X data are the same for each line and that search actually could be over V and save retrieving the XData at all.
[AX,H1,H2]=plotyy(V(1,:),V(2:4,:),V(1,:),V(5:7,:));
...
xlim(AX(1),[1 1.02]); ylim(AX(1),'auto');grid on;
...
axes(AX(1));
xl=xlim;
ix=find(V(1,:)==xl(1),1):find(V(1,:)==xl(2),1,'last');
Ydata=get(H1,'yData');
for ii=1:length(H1)
set(H1(ii),'xData',V(1,ix),'yData',Ydata{ii}(ix})
end
and similarly for H2. I think I got the curlies right for the cell array returned for Y; you don't need any for the xdata as you're reference the raw array directly again. This moves invariant stuff out of the loop as much as possible and eliminates some unneeded temporaries as well.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!