Extracting values from a 3D surface plot for each iteration
7 ビュー (過去 30 日間)
古いコメントを表示
Melecia Senior
2021 年 11 月 12 日
コメント済み: Walter Roberson
2021 年 11 月 12 日
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 件のコメント
採用された回答
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 件のコメント
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
fprintf('\nAfterwards:\n');
Var_z
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!