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 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Stephen23
2024 年 3 月 26 日
編集済み: Stephen23
2024 年 3 月 26 日
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.
採用された回答
Voss
2024 年 3 月 25 日
編集済み: Voss
2024 年 3 月 25 日
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Creating, Deleting, and Querying Graphics Objects についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




