Separating an undetermined cell plot

Hello, I have run into a wall in my code. I am trying to write a function that will take a figure and split the plotted graphs into separate figures. I have an idea of how to do this, but I cannot understand how to split the data I receive from the figures. Can anyone help? I've tried writing a for loop and using cellfun but I can't seem to get it to work.
Here's my code: My goal is to be able to split any size cell into individual cells.
separate_fig('filename')
f = openfig('filename','new','invisible'); %Sets input figure as current without opening it
H = findobj(f, 'type', 'line');
x_data = get(H, 'xdata');
y_data = get(H, 'ydata');
i = cell2mat(x_data); %Data from figure converted from cell into normal array.
j = cell2mat(y_data);

9 comentarios

Walter Roberson
Walter Roberson el 13 de Jun. de 2018
Does the figure have multiple axes (subplot) and each subplot is to be made into its own figure?
Does the figure have any legend or colorbar objects? If so then we will need to know if you are using R2014b or later, or an earlier version.
manta24
manta24 el 13 de Jun. de 2018
That's undetermined, I'm hoping this function will be able to take any figure and split the data to be individually plotted.
It is likely that it will have a legend and colorbar objects. I am using R2018a.
Walter Roberson
Walter Roberson el 13 de Jun. de 2018
The code you posted is headed in the direction of making every single primitive line object into its own figure. It seems unlikely you would want to do that, as it is very common for there to be multiple primitive line objects that need to be considered together, since a single primitive line object can only have one color and one marker shape (and one marker color.) Most plots are composite plots -- for example a contour plot involves a lot of internal line objects (though implemented differently than primitive line objects.)
manta24
manta24 el 13 de Jun. de 2018
I see what you mean. Is there any way I can modify this code to change that?
Walter Roberson
Walter Roberson el 13 de Jun. de 2018
Shrug. You have not given enough information about what should be kept on the same plot. Breaking up by axes would be plausible, as long as plotyy() or equivalent was not used to have multiple overlapping axes.
There might be some semi-tricky parts to move colorbar and legend properly but I don't think it should be too bad.
manta24
manta24 el 13 de Jun. de 2018
Sorry, I am quite new to Matlab. I will try to explain better.
My ultimate goal is to split a given cell data, for example a 16x1 cell, into individual 16x1 cells for individual handles. But I also want the same code to be able to do the same to a 14x1 cell. Each cell I will convert to an array then plot individually on their own figures, but I know how to do that.
If that makes sense, please point me in the direction of which function I could look into using, such as cellfun().
Walter Roberson
Walter Roberson el 16 de Jun. de 2018
Editada: Walter Roberson el 16 de Jun. de 2018
You are getting the data from an existing figure, but you are getting all of it at the same time. To avoid that, we need more information about the data hierarchy. For example, do you need to split axes by axes, each axes becoming a new figure? Or do you need to reach into axes and extract some kind of groupings of lines or surfaces, somehow breaking those out from others in the same axes?
A diagram would help.
Greg
Greg el 19 de Jun. de 2018
Why are you trying to do this?
(Normally, I won't ask why; people tend to get defensive and off-topic. However, sometimes asking why rather than please elaborate gets us better information toward the root of the problem).
Hey Greg, it's just a side project I'm working on. I am trying to better my Matlab skills for classes this upcoming semester. This is one of the problems I am working on to learn.
Thank you both for replying. Walter, my understanding isn't great, but I believe I make the lines in the graph 'objects' then take the handle of them. But I'm not sure if that answers your question.
I have gotten the code to work, however, one of my goals is to automatically detect how many lines I am graphing in the figure. Here is my code, the problem is "for i = 1:100", instead of having my i range from 1:100, I'd like to have some sort of automatic variable to stop the index bounds at the last line handle, if that makes sense.
f = 'filename.fig'
f = openfig(f,'new','invisible');
H = findobj(f, 'type', 'line');
x_data = get(H, 'xdata');
y_data = get(H, 'ydata');
for i = 1:100 %Problem is here
x = x_data(i);
y = y_data(i);
k = cell2mat(x);
j = cell2mat(y);
figure(i);
plot(k,j)
end

Iniciar sesión para comentar.

 Respuesta aceptada

Guillaume
Guillaume el 19 de Jun. de 2018
It seems to me that you do not know how to manipulate cell arrays and perhaps do not understand cell arrays very well (for example your statement of "split a 16x1 cell into individual 16x1 cells" makes no sense, and your using of cell2mat shows you don't know how to index cell arrays).
Going with your current code
...
for i = 1:numel(x_data) %simple way to get the number of elements of a cell array
k = x_data{i}; %no need for cell2mat if you use {} indexing
j = y_data{i};
figure(i);
plot(k, j);
end
However, I would have done it like this:
...
H = findobj(f, 'type', 'line'); %this returns an array of Line
for i = 1:numel(H)
figure(i);
plot(H(i).XData, H(i).YData);
end

3 comentarios

manta24
manta24 el 19 de Jun. de 2018
Hey Guillaume, thanks for your response! Your code works great. You are correct, as I am new to cell arrays. I did not catch the "16x1 into individual 16x1 cells" and simply misspoke, but I will be more careful next time.
Thanks again!
Greg
Greg el 20 de Jun. de 2018
In the interest of learning, nothing beats some manual exploration at the Command Window. Use the environment (MATLAB desktop, plot tab, tab completion, etc.) to your advantage to find out what's available and possible.
To the point of splitting up one figure's children into independent figures, I would take one of the following approaches.
% Approach #1: MOVE objects to the new figure
% (I.e., the original figure ends up empty)
H = findobj(f, 'type', 'line'); %this returns an array of Line
for iline = 1:numel(H)
a = axes(figure(iline));
H(iline).Parent = a;
end
% Approach #2: COPY objects to the new figure
% (I.e., the original figure still has the plot lines)
H = findobj(f, 'type', 'line'); %this returns an array of Line
for iline = 1:numel(H)
a = axes(figure(iline));
copyobj(H(iline),a);
end
Also, you could swap out findobj(...,'line') for f.Children. There will be some differences in the objects returned based on type, handle visibility, etc. - depends what you're going for.
manta24
manta24 el 26 de Jun. de 2018
I agree, using the command window has been great. I am able to test my code and other methods quickly and easily. Thank you for the reply, being able to see two approaches is really helpful!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 13 de Jun. de 2018

Comentada:

el 26 de Jun. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by