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
댓글 수: 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!