As an extra note, the scatter plot does produce the desired result, I was advised to use it as a tool to determine if the data was being manipulated properly and it seems that it is undergoing the correct changes to provide the wanted outcome.
Using surf function with data from excel table
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jackson Pohl
el 25 de Mzo. de 2024
Comentada: Jackson Pohl
el 26 de Mzo. de 2024
I keep getting the "Z must be a matrix, not a scalar or vector" error using the following code, and it is currently reshaped into a matrix using the reshape function. I'm not sure how to convert it to a form the surf function can use:


The ismatrix function produces a result of 1, so Z does identify as a matrix. Does anyone have insight on this?
2 comentarios
Stephen23
el 26 de Mzo. de 2024
Editada: Stephen23
el 26 de Mzo. de 2024
Do NOT change the MATLAB search path just to access data files. Calling ADDPATH and REHASH PATH like that is inefficient. All MATLAB functions which import/export data files accept absolute/relative filenames, so you should just provide the data import/export function with the path. FULLFILE is often useful for that.
Respuesta aceptada
Voss
el 25 de Mzo. de 2024
Editada: Voss
el 25 de Mzo. de 2024
It would help to have the data, in order to know whether the x and y are gridded or scattered.
If they are gridded
[x,y] = ndgrid(linspace(5.4,1,5),linspace(4,10,8));
x = x(:);
y = y(:);
z = rand(numel(x),1);
T = table(x,y,z);
disp(T)
then you can determine the number of grids in the x and y directions and reshape z accordingly before plotting the surface
x = T{:,1};
y = T{:,2};
z = T{:,3};
X = unique(x,'stable');
Y = unique(y,'stable');
Z = reshape(z,numel(X),numel(Y)).'
figure()
surf(X,Y,Z)
hold on
scatter3(x,y,z,32,'r','o','filled')
In this case the scatter points lie exactly on the surface.
On the other hand, if they are scattered
x = 1+4.4*rand(40,1);
y = 4+6*rand(40,1);
z = rand(numel(x),1);
T = table(x,y,z);
disp(T)
then you'll have to do something else, e.g., use a scatteredInterpolant and define a grid on which the Z should be calculated
x = T{:,1};
y = T{:,2};
z = T{:,3};
I = scatteredInterpolant(x,y,z);
X = linspace(min(x),max(x),5);
Y = linspace(min(y),max(y),8);
[X,Y] = meshgrid(X,Y);
Z = I(X,Y)
figure()
surf(X,Y,Z)
hold on
scatter3(x,y,z,32,'r','o','filled')
In this case the scatter points do not lie on the surface because the surface was calculated by interpolation/extrapolation of the data onto a grid.
5 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




