Extracting values from a 3D surface plot for each iteration
7 views (last 30 days)
Show older comments
Melecia Senior
on 12 Nov 2021
Commented: Walter Roberson
on 12 Nov 2021
I have a 3D surface plot (f_plot) of a propagating wave which has 25 time steps. I created a time loop and generated the wave and I want to extract the z value corresponding the x,y values (20,20) in each time step. I want to create a variable (var_z) with these z values for the 20 iterations.
The code i wrote to extract the Z value at each iteration only provides the value for the final iteration. The code is below.
h=surf(f_plot);
var_z = zeros(100,1);
Ts = 25
for n = 1:Ts
m=find((h.XData==20)&(h.YData==20));
Var_z(n)=(h.ZData(m));
end
0 Comments
Accepted Answer
Walter Roberson
on 12 Nov 2021
You do not need to do the surf() in order to find the value.
And you should probably not be assuming that the exact X and Y coordinates exist. Typically you should use interp2() instead.
However, in the context of surf() in which you are only passing in the Z data, then the easiest approach is to skip the surf() and just index the data you would have passed in:
X=20; Y = 20;
f_plot(Y, X) %notice that Y is ROWS not columns !
Anyhow, the problem with your code is that you are not updating the plot between iterations. Your surf() would have to be inside your for n loop to make it worth doing.
2 Comments
Walter Roberson
on 12 Nov 2021
It seems to work when I experiment.
Ts = 4;
X=20; Y = 20;
var_z = zeros(Ts,1);
for n = 1:Ts
f_plot = randi(9, 25, 30);
h = surf(f_plot);
fprintf('for the record, iteration #%d, the pixel is %g\n', n, f_plot(Y, X));
m = find((h.XData==X)&(h.YData==Y));
Var_z(n) = (h.ZData(m));
end
fprintf('\nAfterwards:\n');
Var_z
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements 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!