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
조회 수: 6 (최근 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!




