Exporting data in zoomed in region of MATLAB plot to workspace

883 views Asked by At

I have 4-5 subplots of timeseries data and scatter plots in a MATLAB figure whose x axes are linked. The data is quite long and I've zoomed into a small portion of the figure.Now I want to export just the data contained in this zoomed in portion to work-space as variables. Is it possible?

For example, the following is the plot for the full dataset.

enter image description here

The following is the zoomed in portion,

enter image description here

Now I want to export all the variables or time-section of the variables corresponding to the above zoomed in portion to workspace.

1

There are 1 answers

0
sco1 On

Building off of Ratbert's comment, let's set up a sample plot to play around with.

x = 1:10;
h.myfig = figure();
h.myaxes = axes('Parent', h.myfig);
h.myplot = plot(x);

I'm going to assume you have MATLAB R2014b or newer, which is where MATLAB switched graphics handles to objects. If you have an older version you can swap all of my dot notations with get and set calls, where appropriate.

Now with this initial plot, if we enter h.myaxes.XLim or get(h.myaxes, 'XLim'), we return:

ans =

     1    10

Now if we zoom in arbitrarily and make the same call, we get something different. In my case:

ans =

    3.7892    7.0657

Now it's up to you how you want to use this information to window your data. A very basic method would be to use find to get the indices of the closest data points to these limits.

For example:

newlimits = h.myaxes.XLim;
newminidx = find(x >= floor(newlimits(1)), 1);
newmaxidx = find(x >= ceil(newlimits(2)), 1);

newmin = x(newminidx);
newmax = x(newmaxidx);

Returns [newmin, newmax] of:

ans =

     3     8

I used floor and ceil here because I know my data is integers, your criteria may be different but the process is the same. Hopefully this is enough to get you started.