How to plot isosurfaces using 3 vector columns of data?

I have 4 columns. The first three are the x,y, and z coordinates while the last one is the iso level. How can I plot them in Matlab?. thanks

1 commentaire

what are we supposed to do with the iso level - to do a countour plot ?
you can first plot your x,y,z data as a scatter plot
data = readmatrix('isosurface plot.txt',"NumHeaderLines",8);
figure
scatter3(data(:,1), data(:,2), data(:,3))

Connectez-vous pour commenter.

 Réponse acceptée

Mathieu NOE
Mathieu NOE le 3 Juin 2024
Modifié(e) : Mathieu NOE le 3 Juin 2024
maybe this ?
pretty much what you could obtain with contour (in principe ,but I am not sure how to make contour work on your data)
NB that I extracted the "level" Z data using a certain tolerance (tolZ), then from the raw coordinates I made a closed smoothed curve using smoothn available from the Fex : smoothn - File Exchange - MATLAB Central (mathworks.com)
hope it helps !
data = readmatrix('isosurface plot.txt',"NumHeaderLines",8);
x = data(:,1);
y = data(:,2);
z = data(:,3);
%
figure
scatter3(x, y, z)
hold on
% get level data (here we are dealing with one value only)
level = unique(data(:,4));
level(isnan(level)) = [];
% set a tolerance on the z coordinates
tolZ = level/30;
ind = abs(data(:,3) - level)<tolZ;
xa = data(ind,1);
ya = data(ind,2);
za = data(ind,3);
% select points and re order with theta in ascending order
centroid_x = mean(xa);
centroid_y = mean(ya);
[th,r] = cart2pol(xa-centroid_x,ya-centroid_y);
[th,ia,ic] = unique(th);
r = r(ia);
za = za(ia);
% closing the curve
r(end+1) = r(1);
th(end+1) = th(1)+2*pi;
za(end+1) = za(1);
[xa,ya] = pol2cart(th,r);
xa = xa + centroid_x;
ya = ya + centroid_y;
plot3(xa, ya, za,'*g')
% create smoothed closed curve
A = smoothn({xa, ya, za},10);
xs = A{1};
ys = A{2};
zs = A{3};
% force the smoothed curve to be closed
xs(end+1) = xs(1);
ys(end+1) = ys(1);
zs(end+1) = zs(1);
plot3(xs,ys,zs,'r','linewidth',3)

26 commentaires

Thanks for your reply and help. That is not what I need. You did not use the fourth iso level column. I need the plot shown in the rectangular shape geometry.
indeed, I used the fourth data a(maybe not the way you expected)
% get level data (here we are dealing with one value only)
level = unique(data(:,4));
but there is only a repetition of the same value (0.5) in all rows
% Description: Isosurface
% Xg Yg Zg IsoLevel
1.48 -0.18 -0.60 0.50
1.45 -0.19 -0.56 0.50
1.47 -0.17 -0.60 0.50
1.48 -0.17 -0.61 0.50
1.47 -0.17 -0.61 0.50
1.47 -0.17 -0.60 0.50
1.39 -0.18 -0.42 0.50
1.40 -0.19 -0.40 0.50
1.39 -0.18 -0.39 0.50
1.38 -0.17 -0.40 0.50
...........
so I am back to my first question , what are we supposed to do with it ?
I see your picture , but what is the relation between this and the 0.5 data column ?
I have extracted this data from COMSOL and now want to plot in MATLAB for more good quality graphs. I do not know the relation. It is the isolvel given in comsol data.
So have you succeded in the ploting and getting my graph?. what was the final code then?. please write down it. thanks.
Mathieu NOE
Mathieu NOE le 5 Juin 2024
Modifié(e) : Mathieu NOE le 5 Juin 2024
you could have this result , with the help of this Fex submission :
NB that the fourth iso level column is not needed anyway (for what ?)
%% Load the data
data = readmatrix('isosurface plot.txt',"NumHeaderLines",8);
% Remove rows containing NaN values
data = data(all(~isnan(data), 2), :);
%% Run program
[t,tnorm]=MyRobustCrust(data(:,1:3));
%% plot of the output triangulation
figure(1)
grayColor = [.7 .7 .7];
title('Output Triangulation','fontsize',14)
p = trisurf(t,data(:,1),data(:,2),data(:,3));
p.EdgeColor = 'none';
p.FaceColor = grayColor;
view(-40,24)
box on
camlight(40,40)
camlight(-20,-10)
Please could you make more simple for MATLAB reading?. I want to read a large data using the following way.
fid=fopen('isosurface plot.txt');
ZZ=textscan(fid,'%f %f %f %f','headerlines',9);
fid1=fopen('x distance.txt');
ZZ1=textscan(fid1,'%f %f %f','headerlines',5);
x=ZZ{1};
y=ZZ{2};
z=ZZ{3};
c=ZZ{4};
hello again
sure you can do it also this way (I got exactly the same plot)
i don't know what the second txt file is for ...what do you want to do with the c data ?
%% Load the data
% data = readmatrix('isosurface plot.txt',"NumHeaderLines",8);
fid=fopen('isosurface plot.txt');
ZZ=textscan(fid,'%f %f %f %f','headerlines',8);
% x=ZZ{1};
% y=ZZ{2};
% z=ZZ{3};
data = [ZZ{1} ZZ{2} ZZ{3}];
fclose(fid);
% Remove rows containing NaN values
data = data(all(~isnan(data), 2), :);
%% Run program
[t,tnorm]=MyRobustCrust(data(:,1:3));
%% plot of the output triangulation
figure(1)
grayColor = [.7 .7 .7];
title('Output Triangulation','fontsize',14)
p = trisurf(t,data(:,1),data(:,2),data(:,3));
p.EdgeColor = 'none';
p.FaceColor = grayColor;
view(-40,24)
box on
camlight(40,40)
camlight(-20,-10)
Thank you so much. I am sorry. It was clicked unintentially. You are Great!
ok - my pleasure !
thanks also for the nice comment !!
After runing the code in matlab, why I am getting this error message?. please could eplain it?
Undefined function or variable 'MyRobustCrust'.
Error in Mycode (line 16)
[t,tnorm]=MyRobustCrust(data(:,1:3));
look above , I told you you need to download this fex submission (where the function MyRobustCrust.m is to be used )
I could not succeded with the function "MyRobustCrust". Could we plot without this function?. Actually, my file is on the desktop and MATLAB is reading directly from there. Not is online script.
I attach the function here , save it on your computer and now you should be good
Yes. Great got it. Thank you so much.
runing with magentaColor, i do not get the whole as magenta. why?. see the picture. thaks
Please could you last help me?. I have the 3D geometry data for this drops ploting. I have plotted it but the rectangular geometry is dotted. I need a solid line. Please you could solve this problem?. I have attached the data here.
to your question about the magenta color , you just need
this line : p.FaceColor = grayColor
must be changed into : p.FaceColor = 'm';
then you should get , like me :
now , if you had this line at the end of the code :
shading interp
then you would get this (this is the color scheme of the color map by default ("parula" colormap)
the color is linked to the z value of your data , in relationship to the colormap you have choosen
full code for uniform magenta rendering :
%% Load the data
fid=fopen('isosurface plot.txt');
ZZ=textscan(fid,'%f %f %f %f','headerlines',8);
data = [ZZ{1} ZZ{2} ZZ{3}];
fclose(fid);
% Remove rows containing NaN values
data = data(all(~isnan(data), 2), :);
%% Run program
[t,tnorm]=MyRobustCrust(data(:,1:3));
%% plot of the output triangulation
figure(1)
grayColor = [.7 .7 .7];
title('Output Triangulation','fontsize',14)
p = trisurf(t,data(:,1),data(:,2),data(:,3));
p.EdgeColor = 'none';
% p.FaceColor = grayColor;
p.FaceColor = 'm';
view(-40,24)
box on
camlight(40,40)
camlight(-20,-10)
for your geometry file , the logic is quite similar
so once we have plotted the surface , we add the geometry dots with scatter3 function (you can tweak the color , dot size , etc with the function arguments)
result :
code :
clc
clearvars
%% Load the surface data
fid=fopen('surface plot 1.txt');
Z=textscan(fid,'%f %f %f %f','headerlines',8);
data = [Z{1} Z{2} Z{3}];
fclose(fid);
% Remove rows containing NaN values
data = data(all(~isnan(data), 2), :);
%% Load the geometry data
fid=fopen('geometry.txt');
Z=textscan(fid,'%f %f %f','headerlines',9);
geo = [Z{1} Z{2} Z{3}];
fclose(fid);
% Remove rows containing NaN values
geo = geo(all(~isnan(geo), 2), :);
%% Run program
[t,tnorm]=MyRobustCrust(data(:,1:3));
%% plot of the output triangulation
figure(1)
grayColor = [.7 .7 .7];
title('Output Triangulation','fontsize',14)
p = trisurf(t,data(:,1),data(:,2),data(:,3));
p.EdgeColor = 'none';
p.FaceColor = 'm';
view(-40,24)
box on
camlight(40,40)
camlight(-20,-10)
% now add the geometry dots
hold on
scatter3(geo(:,1),geo(:,2),geo(:,3),5,'filled');
hold off
Thank you so much brother. Allah bless you. Your are really a good man.
as always, my pleasure !
Hey brother, could this works to make this geometry transparent?
gm=mphgeom(model)
mphviewselection(model,gm, 'facealpha', 0.5)
hello again
I suppose so (from the doc it seems doable but Iam not a COMSOL user)
Hello. I have downloaded the smoothn file which works fine for other codes. I have then modified this code but it cannot work. Could you please check and correct it?. Thanks.
clc
clearvars
% Load the surface data
fid=fopen('surface plot 1.txt');
Z=textscan(fid,'%f %f %f %f','headerlines',8);
xx=Z{1};yy=Z{2};zz=Z{3};
Zr=smoothn({xx,yy,zz});
data = [Z{1} Z{2} Z{3}];
fclose(fid);
% Remove rows containing NaN values
data = data(all(~isnan(data), 2), :);
% Load the geometry data
fid=fopen('geometry.txt');
Z=textscan(fid,'%f %f %f','headerlines',9);
geo = [Z{1} Z{2} Z{3}];
fclose(fid);
% Remove rows containing NaN values
geo = geo(all(~isnan(geo), 2), :);
% Run program
[t,tnorm]=MyRobustCrust(data(:,1:3));
% plot of the output triangulation
figure(1)
grayColor = [.7 .7 .7];
title('Output Triangulation','fontsize',14)
p = trisurf(t,data(:,1),data(:,2),data(:,3),Zr{1},Zr{2},Zr{3});
p.EdgeColor = 'none';
p.FaceColor = 'b';
view(-40,24)
box on
camlight(40,40)
camlight(-20,-10)
hold on
% now add the geometry dots
scatter3(geo(:,1),geo(:,2),geo(:,3),5,'filled');
hold on
hello
I don't thinl smoothn is appropriate here for scattered data representing a closed surface - I got a bad result here - this is how I changed a bit your code :
clc
clearvars
%% Load the surface data
fid=fopen('surface plot 1.txt');
Z=textscan(fid,'%f %f %f %f','headerlines',8);
data = [Z{1} Z{2} Z{3}];
fclose(fid);
% Remove rows containing NaN values
data = data(all(~isnan(data), 2), :);
% try do some smoothing
xx=data(:,1);yy=data(:,2);zz=data(:,3);
Zr=smoothn({xx,yy,zz});
data = [Zr{1} Zr{2} Zr{3}];
%% Load the geometry data
fid=fopen('geometry.txt');
Z=textscan(fid,'%f %f %f','headerlines',9);
geo = [Z{1} Z{2} Z{3}];
fclose(fid);
% Remove rows containing NaN values
geo = geo(all(~isnan(geo), 2), :);
%% Run program
[t,tnorm]=MyRobustCrust(data(:,1:3));
%% plot of the output triangulation
figure(1)
grayColor = [.7 .7 .7];
title('Output Triangulation','fontsize',14)
p = trisurf(t,data(:,1),data(:,2),data(:,3));
p.EdgeColor = 'none';
p.FaceColor = 'm';
view(-40,24)
box on
camlight(40,40)
camlight(-20,-10)
% now add the geometry dots
%
hold on
scatter3(geo(:,1),geo(:,2),geo(:,3),5,'filled');
hold off
I aleady made some suggestions above
this also may interest you if you need to smooth the surface
Ok brother. Thank you so much.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by