How to surfc or surf a .mat file

조회 수: 5 (최근 30일)
Marian
Marian 2025년 8월 20일
답변: Walter Roberson 2025년 8월 20일
Hi! I am trying to import a .mat file from a folder and trying to display through surf. But I am getting this error.

답변 (2개)

Star Strider
Star Strider 2025년 8월 20일
편집: Star Strider 2025년 8월 20일
The error is clear --
imshow(imread('64C9C921-689D-4180-8EAD-7EE1126E5382.png'))
At least one of your data are vectors (it would nelp to have them). There are several ways to create matrices from vectors, depending on what the original data are. The easiest way to determine that is to plot all three vectors using the stem3 function and see what the result looks like. If the data appear to be gridded (regularly spaced, forming a sampled surface), use the reshape function to shape them into matrices. If they are not, then the vectors will have to be interpolated to matrices. The best way to do that is with the scatteredInterpolant function.
Regularly-spaced (gridded) vectors --
x = linspace(-5, 5, 10);
y = linspace(0, 11, 10);
[xm,ym] = ndgrid(x,y);
zm = exp(-(xm.^2 + (ym-5).^2)/10);
xyz = [xm(:) ym(:) zm(:)]; % Vectors
figure
stem3(xyz(:,1), xyz(:,2), xyz(:,3), 'filled')
X = reshape(xyz(:,1), 10, []);
Y = reshape(xyz(:,2), 10, []);
Z = reshape(xyz(:,3), 10, []);
figure
surfc(X, Y, Z)
colormap(turbo)
colorbar
Randomly-spaced vectors --
x = randi([-500, 500], 15, 1)/100;
y = randi([0, 1100], 15, 1)/100;
[xm,ym] = ndgrid(x,y);
zm = exp(-(xm.^2 + (ym-5).^2)/10);
xyz = [xm(:) ym(:) zm(:)]; % Vectors
figure
stem3(xyz(:,1), xyz(:,2), xyz(:,3), 'filled')
F1 = scatteredInterpolant(xyz(:,1), xyz(:,2), xyz(:,3))
F1 =
scatteredInterpolant with properties: Points: [225×2 double] Values: [225×1 double] Method: 'linear' ExtrapolationMethod: 'linear'
x = linspace(-5, 5, 25);
y = linspace(0, 11, 25);
[X, Y] = ndgrid(x, y);
Z = F1(X, Y);
figure
surfc(X, Y, Z)
colormap(turbo)
colorbar
.
EDIT -- (20 Aug 2025 at 05:59)
Added example code.
.

Walter Roberson
Walter Roberson 2025년 8월 20일
The output of load() from a .mat file, is a structure, with one field for each variable that is loaded. You need to extract the necessary parts of the variable from the structure. For example,
cell1 = load('abcdef.mat').XData
if the variable was stored in XData in file abcdef.mat

카테고리

Help CenterFile Exchange에서 Vector Volume Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by