How to surfc or surf a .mat file

4 ビュー (過去 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 Center および File ExchangeVector Volume Data についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by