Using surf function with data from excel table

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Jackson Pohl
Jackson Pohl il 25 Mar 2024
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.
Stephen23
Stephen23 il 26 Mar 2024
Modificato: Stephen23 il 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.

Accedi per commentare.

Risposta accettata

Voss
Voss il 25 Mar 2024
Modificato: Voss il 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)
x y z ___ ______ _________ 5.4 4 0.20813 4.3 4 0.12496 3.2 4 0.39786 2.1 4 0.71341 1 4 0.8041 5.4 4.8571 0.50956 4.3 4.8571 0.31989 3.2 4.8571 0.0053885 2.1 4.8571 0.75143 1 4.8571 0.86634 5.4 5.7143 0.52135 4.3 5.7143 0.17024 3.2 5.7143 0.30531 2.1 5.7143 0.42971 1 5.7143 0.41343 5.4 6.5714 0.01241 4.3 6.5714 0.3412 3.2 6.5714 0.73762 2.1 6.5714 0.181 1 6.5714 0.70824 5.4 7.4286 0.89059 4.3 7.4286 0.48137 3.2 7.4286 0.29383 2.1 7.4286 0.027363 1 7.4286 0.63855 5.4 8.2857 0.26877 4.3 8.2857 0.44245 3.2 8.2857 0.71812 2.1 8.2857 0.98614 1 8.2857 0.69864 5.4 9.1429 0.39224 4.3 9.1429 0.24801 3.2 9.1429 0.88306 2.1 9.1429 0.3135 1 9.1429 0.047909 5.4 10 0.028884 4.3 10 0.23169 3.2 10 0.59952 2.1 10 0.6228 1 10 0.33481
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)).'
Z = 8x5
0.2081 0.1250 0.3979 0.7134 0.8041 0.5096 0.3199 0.0054 0.7514 0.8663 0.5214 0.1702 0.3053 0.4297 0.4134 0.0124 0.3412 0.7376 0.1810 0.7082 0.8906 0.4814 0.2938 0.0274 0.6385 0.2688 0.4424 0.7181 0.9861 0.6986 0.3922 0.2480 0.8831 0.3135 0.0479 0.0289 0.2317 0.5995 0.6228 0.3348
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)
x y z ______ ______ ________ 2.9591 7.1576 0.31834 4.7758 9.3023 0.065207 4.6927 8.1744 0.66528 3.1441 7.8177 0.79885 3.8019 4.0164 0.55441 5.363 4.4674 0.13931 4.8018 9.0908 0.054529 5.226 9.1488 0.48932 2.3734 7.6413 0.39042 3.5804 4.7519 0.35436 3.7212 7.6837 0.58757 1.0912 5.0552 0.3594 1.427 8.9829 0.59153 2.7388 4.0643 0.57094 2.758 4.2733 0.39638 1.7745 4.1681 0.81471 4.2311 7.0143 0.54371 4.6068 8.5291 0.97366 3.4731 6.1257 0.15647 1.2959 9.2176 0.013094 3.7287 6.3124 0.73943 1.069 8.3555 0.60209 2.0307 9.1535 0.91088 3.3958 4.9858 0.6511 4.3794 6.109 0.8781 2.7535 7.2607 0.35717 1.8027 5.773 0.30448 1.6564 7.2397 0.49787 4.9911 8.6641 0.14537 3.8204 7.4689 0.72007 1.0116 5.1652 0.89224 3.8024 7.7002 0.59089 2.7738 5.607 0.1959 4.8534 9.7478 0.13545 1.8173 6.1251 0.19459 2.8249 9.3558 0.9205 2.8219 7.0614 0.96915 5.2179 4.6159 0.24682 4.1396 9.2608 0.82085 3.2687 7.3872 0.93174
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)
Z = 8x5
0.6661 0.8074 0.5805 0.4482 0.0005 0.1862 0.5507 0.5549 0.3658 0.1641 0.8704 0.2928 0.2902 0.6973 0.2275 0.8163 0.4408 0.4783 0.7391 0.3024 0.7404 0.4393 0.7430 0.5872 0.3888 0.6426 0.4838 0.8146 0.7551 0.4867 0.0721 0.8413 0.8673 0.8583 0.5960 -1.2803 -0.5411 -0.1017 0.1129 0.2303
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 Commenti
Voss
Voss il 26 Mar 2024
Please share the code you are running.
Jackson Pohl
Jackson Pohl il 26 Mar 2024
I actually ended up figuring it out! Although now I am having trouble establishing a gradient but that's for a different post...

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by