Separating an undetermined cell plot

조회 수: 3 (최근 30일)
manta24
manta24 2018년 6월 13일
댓글: manta24 2018년 6월 26일
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
Greg
Greg 2018년 6월 19일
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 2018년 6월 19일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2018년 6월 19일
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
Greg
Greg 2018년 6월 20일
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 2018년 6월 26일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by