Extracting values from a 3D surface plot for each iteration

7 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2021-11-12
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 个评论
Melecia Senior
Melecia Senior 2021-11-12
Even when i put the surf () within the for n loop it still only returns one value Var_z
Walter Roberson
Walter Roberson 2021-11-12
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
for the record, iteration #1, the pixel is 7 for the record, iteration #2, the pixel is 4 for the record, iteration #3, the pixel is 1
for the record, iteration #4, the pixel is 2
fprintf('\nAfterwards:\n');
Afterwards:
Var_z
Var_z = 1×4
7 4 1 2

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by