extract fig into surf(x,y,z)
Show older comments
There's a .fig that plots n z(x) curves, each of which corresponds to 1 y value (n=3 in the illustration). The y values are known data. I don't have those z(x) data besides this .fig. How to convert this fig into a surf(x,y,z) plot?

5 Comments
Do you have an actual .fig file, or do you just have this sketch?
Even if you have a .fig file with internal data, it may not contain all the data necessary to unambiguously reconstruct the 2D function it represents. Consider the example:
% some 2D function
x = linspace(0,2*pi,100); % uniformly-spaced xdata
y = [0 0.4 0.6 0.75 0.9 1.05 1.2].'; % arbitrarily-spaced ydata
Z = sin(x).*cos(y); % Z = f(x,y)
% this is the surf that we might want
subplot(1,2,1)
surf(x,y,Z)
% but this is the only object we have
% if this were the only thing in the .fig,
% then the ydata would be lost.
subplot(1,2,2)
plot(x,Z)
grid on
xlim([0 2*pi])
In this example, our simple line plot represents a 2D surface, but the object created by plot() does not actually contain the ydata. Unless there were a legend or something which specifies the y-position of each of these curves, we can't know what they were. There may be exceptions in cases where we know some information about the function being described, but I'm going to assume we don't.
In order to know if the question is answerable, we need to know what information is available.
feynman feynman
on 4 Dec 2024
I reduced part of the above example to a fig file with nothing other than the 2D line part:
% say we have a fig file with some 2D line plots
hfig = openfig('stuff.fig');
% assuming it contains a number of 2D line objects
hl = findobj(hfig,'type','line');
% let's say we knew what the ydata was
y = [0 0.4 0.6 0.75 0.9 1.05 1.2].';
% assuming the xdata,zdata length is consistent across all line objects
xlen = numel(hl(1).XData);
nlines = numel(hl);
X = zeros(nlines,xlen);
for k = 1:nlines
ki = nlines - k + 1; % expect object order to be reversed
X(k,:) = hl(ki).XData;
Z(k,:) = hl(ki).YData;
end
% go ahead and expand y in the case that all the xdata vectors are not identical
Y = repmat(y,[1 xlen]);
% make a new surf plot
figure
surf(X,Y,Z)
That gives us the same surf plot that we had before. If the data series aren't the same length, you might need to jump through a few other hoops.
feynman feynman
on 29 Dec 2024
Edited: feynman feynman
on 29 Dec 2024
Walter Roberson
on 29 Dec 2024
If hfig is dots but the code succeeds in creating a surface, then the implication is that the dots were created as line() objects with 'linestyle', 'none' instead of being created as scatter() objects. Creating dots as line() objects instead of as scatter() objects is fairly common.
The code gathers all of the XData and YData of the line() objects and ignores what the 'linestyle' is set to for the line() objects.
Accepted Answer
More Answers (0)
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




