How do I plot a 3-d matrix.( error with surf)

조회 수: 6 (최근 30일)
savitha muthanna
savitha muthanna 2021년 4월 20일
답변: Hitesh 2024년 8월 29일
I have a 321x60x120 matrix. The matrix has positive and negative float values. I would like to plot it. I tried surf. I get an error saying : "Error using matlab.graphics.chart.primitive.Surface
Value must be a scalar, vector or array of numeric type.". Values are all numeric. What could be wrong?
Is there any other plotting function, that would be recommended. The values are positions and momentums of a 2-d system, that evolve over time. time is the first dimension. The first 60 in the 3rd dimension are positions and the next 60 are momentum of this 2-d system. Ideally, what I want to do, is plot, for each iteration, a 3-d plot for each set of 60 rows and 60 columns of position and then a 3-d plot for each set of 60 rows and 60 positions of momentum, and show the evolution over time.
  댓글 수: 3
savitha muthanna
savitha muthanna 2021년 4월 20일
편집: savitha muthanna 2021년 4월 20일
isosurface plots isosurface data from volume data - is it equivalent to surf? And what does the error mean?
Rafael Hernandez-Walls
Rafael Hernandez-Walls 2021년 4월 20일
doc slice

댓글을 달려면 로그인하십시오.

답변 (1개)

Hitesh
Hitesh 2024년 8월 29일
Hi Savitha,
The error you receive Error using matlab.graphics.chart.primitive.Surface suggests that the input that you passed as a parameter does not match specified dimension. You receive this error because you are passing a 3D matrix, whereas surf is allowed to only take 2D matrix as an input.
However, I understand that you are trying to visualize the evolution of position and momentum of a 2D system over time. For that you need to extract 2D slices from your 3D matrix. Since your matrix is 321x60x120, you can think of it as 321-time steps, with each time step containing a 60x120 matrix. This 60x120 matrix can be split into two 60x60 matrices: one for position and one for momentum.
Please refer to below code for reference:
data = rand(321, 60, 120);
for t = 1:size(data, 1)
% Extract the position and momentum matrices
positionMatrix = data(t, :, 1:60);
momentumMatrix = data(t, :, 61:120);
% Reshape to 60x60 matrices
positionMatrix = reshape(positionMatrix, [60, 60]);
momentumMatrix = reshape(momentumMatrix, [60, 60]);
% Plot positions
subplot(1, 2, 1);
surf(positionMatrix);
title(['Positions at time step ', num2str(t)]);
xlabel('X');
ylabel('Y');
zlabel('Position');
shading interp;
% Plot momentums
subplot(1, 2, 2);
surf(momentumMatrix);
title(['Momentums at time step ', num2str(t)]);
xlabel('X');
ylabel('Y');
zlabel('Momentum');
shading interp;
% Pause for animation effect
pause(0.1);
end

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by