extract fig into surf(x,y,z)

29 views (last 30 days)
feynman feynman
feynman feynman on 4 Dec 2024 at 11:35
Commented: DGM on 5 Dec 2024 at 19:48
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?
  3 Comments
feynman feynman
feynman feynman on 4 Dec 2024 at 13:50
Thanks a lot. Let's assume that the y values are known besides the .fig (not a sketch) data.
DGM
DGM on 5 Dec 2024 at 19:48
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.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 4 Dec 2024 at 12:34
One way is tto enlarge the y-values to a matrix using repmat and then offset them by adding a different constant to each column (in this example).
x = linspace(0, 1).';
z = sin(2*pi*x*(1:3));
y = ones(size(z,1),1);
xm = repmat(x, 1, size(z,2));
ym = repmat(y, 1, size(z,2));
figure
plot3(xm, ym, z)
grid
colormap(turbo)
title('‘plot3’ Using ‘Original’ Y-Values')
figure
surf(xm, ym, z)
grid on
colormap(turbo)
title('‘surf’ Using ‘Original’ Y-Values')
ym = ym + (0:2); % Create ‘Offset’ ‘Y’-Values
figure
surf(xm, ym, z)
grid on
colormap(turbo)
title('‘surf’ Using ‘Offset’ Y-Values')
.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!