How to surfc or surf a .mat file
2 views (last 30 days)
Show older comments
Hi! I am trying to import a .mat file from a folder and trying to display through surf. But I am getting this error.
0 Comments
Answers (2)
Star Strider
on 20 Aug 2025
Edited: Star Strider
on 20 Aug 2025
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))
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.
.
0 Comments
Walter Roberson
on 20 Aug 2025
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
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!