- Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
- Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
- Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support using the telephone icon in the upper-right corner of this page so we can investigate.

# Problem with reshape and Surf plot

6 views (last 30 days)

Show older comments

Hello MATLAB Community,

I have the following script and data where i would like to plot a 3d Surface and it works well for some data and it doesn't work because of the dimensions in some cases. Can someone please suggest where is the mistake in the script and how to fix it?

opts = detectImportOptions('Data123.xlsx');

opts = opts.setvartype('Y','double');

opts = opts.setvartype('Z','double');

% Load data

data = readtable('Data123.xlsx', opts);

data.Properties.VariableNames = ["X","F_sw","Y","Z"];

% order the data

data = sortrows(data,["X","Y"]);

X = unique(data.X);

Y = unique(data.Y);

Z = reshape(data.Z,[length(Y),length(X)]);

% View

figure(1)

scatter3(data.X,data.Y,data.Z,12,[1 0 0],"filled","MarkerEdgeColor",[0 0 0])

hold on

s = surf(X,Y,Z,"FaceColor",'interp');

hold off

##### 6 Comments

### Accepted Answer

Star Strider
on 27 Jul 2021

Try this —

opts = detectImportOptions('https://www.mathworks.com/matlabcentral/answers/uploaded_files/695269/Data123.xlsx');

opts = opts.setvartype('Y','double');

opts = opts.setvartype('Z','double');

% Load data

data = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/695269/Data123.xlsx', opts)

data.Properties.VariableNames = ["X","F_sw","Y","Z"];

% order the data

data = sortrows(data,["X","Y"]);

% X = unique(data.X);

% Y = unique(data.Y);

X = data.X;

Y = data.Y;

Z = data.Z;

N = 50;

xv = linspace(min(X), max(X), N);

yv = linspace(min(Y), max(Y), N);

[Xm,Ym] = ndgrid(xv, yv);

Zm = griddata(X,Y,Z,Xm,Ym);

% Z = reshape(data.Z,[length(Y),length(X)]);

% View

figure(1)

scatter3(data.X,data.Y,data.Z,12,[1 0 0],"filled","MarkerEdgeColor",[0 0 0])

hold on

s = surf(Xm,Ym,Zm,"FaceColor",'interp', 'EdgeColor','none');

hold off

If it is not the result you want, I will delete my Answer.

.

##### 4 Comments

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!