Separating an undetermined cell plot
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
manta24
el 13 de Jun. de 2018
Comentada: manta24
el 26 de Jun. de 2018
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
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).
Respuesta aceptada
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
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!