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
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Stephen23
le 26 Mar 2024
Modifié(e) : Stephen23
le 26 Mar 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.
Réponse acceptée
Voss
le 25 Mar 2024
Modifié(e) : Voss
le 25 Mar 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!