Plot a surface with X Y Z data

I am importing three different data sets from excel sheet and I want to plot the latitude, longitude on x and y axis and energy on z axis.
Whenever I import the three data-sets and use the surface function, matlab displays an error that Z must be a matrix not a scalar or vector. I am unable to rectify this as I feel the imported data is already in the form of a matrix.
Please suggest the edit to rectify the error.
PROGRAM:
close all
clear all
clc
[~,long_energy] = xlsread('Energy','B:B');
[~,lat_energy] = xlsread('Energy','A:A');
EE = xlsread('Energy', 'J:J');
lat = str2double(lat_energy);
long = str2double(long_energy);
surf(lat,long, EE);

3 Commenti

Could you please upload your file "Energy" so we could take a look?
Ujjwal
Ujjwal il 15 Lug 2014
Yes. First column is Latitude, second is Longitude and the last is Energy.
Ujjwal
Ujjwal il 15 Lug 2014
The file.

Accedi per commentare.

 Risposta accettata

num = xlsread('Energy.xlsx');
long_energy = num(:,2);
lat_energy = num(:,1);
EE = num(:,end);
F = scatteredInterpolant(long_energy, lat_energy, EE);
min_long = min(long_energy);
min_lat = min(lat_energy);
max_long = max(long_energy);
max_lat = max(lat_energy);
proj_long = linspace(min_long, max_long, 100);
proj_lat = linspace(min_lat, max_lat, 100);
[PROJ_LONG, PROJ_LAT] = ndgrid(proj_long, proj_lat);
PROJ_EE = F(PROJ_LONG, PROJ_LAT);
surf(PROJ_LONG, PROJ_LAT, PROJ_EE, 'edgecolor', 'none');

1 Commento

I tried this approah with my own data, but when using surf, I still get the error "Z must be a matrix, not a scaler or vector".

Accedi per commentare.

Più risposte (7)

x=[32 20 67 1 98 34 57 65 24 82 47 55 8 51 13 14 18 30 37 39 10 33 21 26 38 81 83 60 95 22 17 5 72 46 99 52 12 25 96 29 70 85 43 69 19 78 97 31 89 53 2 91 48 71 61 15 36 84 94 50 11 80 6 7 49 74 9 88 40 79 27 68 73 64 63 59 86 23 35 58 45 28 100 42 93 87 16 90 41 66 54 92 77 4 62 76 75 56 3 44];
y=[96 75 24 9 83 49 27 77 3 23 17 31 40 13 7 52 51 21 98 47 64 79 78 91 44 16 15 100 84 99 63 68 70 30 54 76 97 73 33 5 88 8 71 66 62 25 60 42 72 45 18 11 28 59 89 65 10 55 69 81 12 26 20 95 87 41 74 50 93 22 43 90 14 34 82 35 56 38 80 32 1 57 6 36 37 61 29 58 2 48 4 46 67 53 92 86 94 19 39 85];
z=[55 31 11 45 83 36 86 49 15 57 42 46 8 94 88 47 54 81 98 41 32 35 56 85 9 89 37 60 23 62 67 100 78 76 73 80 10 20 68 34 77 93 1 63 53 12 22 99 91 40 84 24 33 3 43 19 92 97 6 82 64 25 26 79 95 4 44 58 5 21 70 29 65 87 96 90 51 14 18 2 72 28 71 39 52 7 27 59 50 61 48 30 66 69 17 13 74 16 75 38];
xlin = linspace(min(x), max(x), 100);
ylin = linspace(min(y), max(y), 100);
[X,Y] = meshgrid(xlin, ylin);
% Z = griddata(x,y,z,X,Y,'natural');
% Z = griddata(x,y,z,X,Y,'cubic');
Z = griddata(x,y,z,X,Y,'v4');
mesh(X,Y,Z)
axis tight; hold on
plot3(x,y,z,'.','MarkerSize',15)
Toby
Toby il 10 Ott 2017
It sounds like you're looking for
scatter3(x,y,z)
https://www.mathworks.com/help/matlab/ref/scatter3.html

2 Commenti

Yes! scatter3(x,y,z) is indeed what i was looking for, thank you!
Yohana
Yohana il 2 Gen 2023
Attempt with Mat lab to produce the results shown on slides 30, 32, 37, 38, 43, 44, 48, 52, 56, 57, 59, 60, 70, 71, 72.

Accedi per commentare.

When x, y and z are vector, you can't use surf(x,y,z). x,y and z should be matrices of the same size look at surf function. What you can do with your vectors is
plot3(lat,long, EE)

2 Commenti

Ujjwal
Ujjwal il 15 Lug 2014
Using the plot function, I get lines which is not really a kind of result I am looking for. I want to portray the result using a surface.
All you need to do for that is specifying '.' as your marker:
plot3(lat,long, EE,'.')
The rest is also just customizing your plot.

Accedi per commentare.

Joseph Cheng
Joseph Cheng il 15 Lug 2014
Modificato: Joseph Cheng il 15 Lug 2014

0 voti

you can try to use the interp2() function. I haven't checked your excel file but it may accomplish what you're looking for. by using your data and attempting to put it in a meshgrid format.
Kenzai
Kenzai il 4 Ott 2017

0 voti

Is this question ever been answered ? Because I'm stuck on a likewise problem. Thanks ahead!

1 Commento

Yohana
Yohana il 2 Gen 2023
Attempt with Mat lab to produce the results shown on slides 30, 32, 37, 38, 43, 44, 48, 52, 56, 57, 59, 60, 70, 71, 72.
Help me please

Accedi per commentare.

Shubham Agrawal
Shubham Agrawal il 8 Ott 2017

0 voti

bump, same question - what's the best way to plot a set of X, Y and Z data?
Sheriza
Sheriza il 1 Set 2022

0 voti

x = [0 5 10 15 20 26 27 28 30 30 30 30 30 25 20 15 10 5 0 0 0 0 5 10 15 20 25 25 25 20 15 12 10 5 5 10 14 15 20];
y=[0 0 0 0 0 0 0 0 0 5 10 15 20 20 20 20 20 20 20 15 10 5 5 5 5 5 5 10 15 15 15 15 15 15 10 10 10 10 10];
Z=[13.08 13.74 11.43 22.34 25.74 20.00 12.89 26.41 19.36 19.75 17.08 22.52 21.70 18.29 25.68 24.90 22.78 12.47 17.80 13.13 20.23 14.53 10.78 17.95 22.40 15.15 10.18 14.75 19.15 14.34 12.77 23.95 24.35 4.92 18.11 14.08 24.95 22.48 18.63];
xlin = linspace(min(x), max(x), 100);
ylin = linspace(min(y), max(y), 100);
[X,Y] = meshgrid(xlin, ylin);
% Z = griddata(x,y,z,X,Y,'natural');
% Z = griddata(x,y,z,X,Y,'cubic');
Z = griddata(x,y,z,X,Y,'v4');
mesh(X,Y,Z)
axis tight; hold on
plot3(x,y,z,'.','MarkerSize',15)

Categorie

Scopri di più su 2-D and 3-D Plots in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by