Filter löschen
Filter löschen

Extracting values from a 3D surface plot for each iteration

7 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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 Kommentare
Melecia Senior
Melecia Senior am 12 Nov. 2021
Even when i put the surf () within the for n loop it still only returns one value Var_z
Walter Roberson
Walter Roberson am 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
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by