Filter löschen
Filter löschen

Separating an undetermined cell plot

1 Ansicht (letzte 30 Tage)
manta24
manta24 am 13 Jun. 2018
Kommentiert: manta24 am 26 Jun. 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 Kommentare
Greg
Greg am 19 Jun. 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).
manta24
manta24 am 19 Jun. 2018
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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 19 Jun. 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 Kommentare
Greg
Greg am 20 Jun. 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 am 26 Jun. 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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by