How to plot 3d from scattered dataset captured from xlsx file?

Hi.
I have xlsx file ... I'm able to plot it as 2d ... But I want to have the 3d version...
I have attached a sample of the dataset and how the figure looks in 2d and how it should look in 3d diemension
I copied the data to txt file and tried to plot it using the code below, but it didnt work with me
Any suggestions?
Many Thanks
data = dlmread('sample.txt');
x = data(:,1);
y = data(:,2);
z = data(:,3);
figure
surf(reshape(x,1,[]),reshape(y,1,[]),reshape(z,1,[]))

 Accepted Answer

You need gridded data to use surf. You have scattered data.
xyz = readmatrix('sample.xlsx');
[x,y,z] = deal(xyz(:,1),xyz(:,2),xyz(:,3));
[X,Y] = meshgrid(linspace(min(x),max(x),100),...
linspace(min(y),max(y),100));
Z = griddata(x,y,z,X,Y,'linear');
%extrapolate using constant = 0
Z(isnan(Z)) = 0;
surf(X,Y,Z)
Alternatively you could fit a surface to your contours using this FEX function
Also, you probably want to interpolate your data to get more samples, as the resulting fit is very ugly. Could do something like:
t = 1:numel(x);
k = 2;
tv = linspace(min(t),max(t),numel(x)*k);
x = interp1(t,x,tv);
y = interp1(t,y,tv);
z = interp1(t,z,tv);

7 Comments

I really appreciate your help!. Many thanks
BTW: I'm trying to plot the "failure envelope" for a friend of mine
I don't know really what this data represents.
However, is there a way to exclude those values which almost approach 0?
I can do this manually, but its tiresome procedure...
I have attached the figure in this comment, kindly have a look.
Thanks again
My pleasure!
Just exclude thus line from the code
Z(isnan(Z)) = 0;
griddata does not extrapolate, at least not by default
Thanks.... I also excluded them by using this:
Z(isnan(Z)) ~= 0;
Hmm.. I dont think that statement actually does anything though :)
If im not mistaken it will just return an array of logical true
Thanks a lot... both of them worked find with me :)
Many thanks for your response... you saved my friend's day!
That is unrelated to OP's question, so you should start a separete one :)
data = cat(1, image_patches,labels);
That code is overwriting all of data each iteration.
It looks to me as if data will not be a vector, but I do not seem to be able to locate any hellopatches() function so I cannot tell what shape it will be. As you are not doing imresize() I also cannot be sure that all of the images are the same size, so I cannot be sure that data will be the same size for each iteration. Under the circumstances you should be considering saving into a cell array.
Note: please do not post the same query multiple times. I found at least 9 copies of your query :(

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Asked:

on 14 Jul 2020

Commented:

on 15 Jul 2020

Community Treasure Hunt

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

Start Hunting!