Separating an undetermined cell plot
1 次查看(过去 30 天)
显示 更早的评论
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
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).
采纳的回答
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
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!