MATLAB Answers

Plot a surface with X Y Z data

406 views (last 30 days)
Ujjwal on 15 Jul 2014
Commented: Jason Burke on 4 May 2021
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.
close all
clear all
[~,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);

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 11 Oct 2017
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);
surf(PROJ_LONG, PROJ_LAT, PROJ_EE, 'edgecolor', 'none');
  1 Comment
Jason Burke
Jason Burke on 4 May 2021
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".

Sign in to comment.

More Answers (5)

Azzi Abdelmalek
Azzi Abdelmalek on 15 Jul 2014
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)
  1 Comment
Ujjwal on 15 Jul 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.

Sign in to comment.

Joseph Cheng
Joseph Cheng on 15 Jul 2014
Edited: Joseph Cheng on 15 Jul 2014
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 on 4 Oct 2017
Is this question ever been answered ? Because I'm stuck on a likewise problem. Thanks ahead!

Shubham Agrawal
Shubham Agrawal on 8 Oct 2017
bump, same question - what's the best way to plot a set of X, Y and Z data?

Toby on 10 Oct 2017
It sounds like you're looking for
  1 Comment
Gennadij Nikitin
Gennadij Nikitin on 18 Sep 2019
Yes! scatter3(x,y,z) is indeed what i was looking for, thank you!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!