How to export 3D data from Matlab as a stl or obj file

81 次查看(过去 30 天)
Hi,
I am new in MatLab and use it to create visual graphics for my work and never worked with the generated code or wrote code for matlab till now.
I generated the graphic below, which I need to print with 3D printer. There is no connection to the antennas, I just use the visual form. I need it in .stl or .obj file format so I can import in in the 3D printer program.
I red about stlwrite function in this forum and tried to use it in the generated code of my graphic, but unfortunately it is not possible for me to generate the .stl file on this way.
Could you please help me to have this in .stl format?
Here is the graphic:
this is the generated code of it:
%MATLAB Code from Sensor Array Analyzer App
%Generated by MATLAB 8.4 and Phased Array System Toolbox 2.3
%Generated on 16-May-2016 17:17:33
addpath('E:\_matlab stuff\stlwrite')
% Create a uniform hexagonal array
rows = [1:4 3:-1:1];
Radius = 1*(3);
d = 1;
pos = zeros(3,1);
count = 0;
for idx = 1:length(rows)
y = -Radius/2 -(rows(idx)-1)*d*0.5:d: Radius/2+(rows(idx)-1)*d*0.5;
pos(2, count+1:count+length(y)) = y;
pos(3, count+1:count+length(y)) = sqrt(3)/2*Radius - (idx-1)*d*sind(60);
count = count + length(y);
end
h = phased.ConformalArray('ElementPosition', pos, 'ElementNormal', ...
zeros(2, size(pos, 2)));
h.Element = ...
phased.IsotropicAntennaElement;
%Assign frequencies and propagation speed
F = 300000000;
PS = 300000000;
%Plot the directivity pattern
pr = plotResponse(h, F(1), PS, 'Unit','dbi', 'Format', 'Polar', ...
'RespCut', '3D');
set(pr, 'LineStyle', 'none');
%Adjust the view angles
view(25, 25);
%Get the title
title = get(gca, 'title');
title_str = get(title, 'String');
%Modify the title
[Fval, ~, Fletter] = engunits(F(1));
title_str = [title_str sprintf('\n') ...
num2str(Fval) ' ' Fletter 'Hz No Steering'];
set(title, 'String', title_str);
-----------------
thank you very much in advance
Dimi
  1 个评论
A D
A D 2016-10-27
移动:DGM 2025-4-5
Hello Dimi, I also am struggling with these error messages. Did you succeed in exporting the .fig to .stl or .obj? If so, would you be willing to share your process? Thank you for your time.

请先登录,再进行评论。

回答(2 个)

Honglei Chen
Honglei Chen 2016-5-17
I have never done this so I cannot guarantee the workflow. However, the figure generated by the above code is a surf plot and the following File Exchange submission claims that it can translate surf data to .stl file.
HTH
  4 个评论
Yaroslav Rybka
Yaroslav Rybka 2019-3-3
I know this is an old and probably irrelevant at this point for the OP, but for all those who find this thread in the future:
What you should have in the variable 'h', according to the documentation is the surface handle. If not, the handle could be retrieved with :
h = gco;
This handle can provide access to the coordinates you need, all you have to do is reference the right fields:
X = h.XData;
Y = h.YData;
Z = h.ZData;
It's useful to remember that even with only the plot of the data, you can retrieve or update the said data .
DGM
DGM 2025-9-27,1:49
If you can use surf2stl(), you can just use surf2patch() and use any competent STL encoder to do the job. Things have changed since 2016, but in modern versions, no third party tools are needed.
% Create a uniform hexagonal array
rows = [1:4 3:-1:1];
Radius = 1*(3);
d = 1;
pos = zeros(3,1);
count = 0;
for idx = 1:length(rows)
y = -Radius/2 -(rows(idx)-1)*d*0.5:d: Radius/2+(rows(idx)-1)*d*0.5;
pos(2, count+1:count+length(y)) = y;
pos(3, count+1:count+length(y)) = sqrt(3)/2*Radius - (idx-1)*d*sind(60);
count = count + length(y);
end
h = phased.ConformalArray('ElementPosition', pos, 'ElementNormal', zeros(2, size(pos, 2)));
h.Element = phased.IsotropicAntennaElement;
% Assign frequencies and propagation speed
F = 300000000;
PS = 300000000;
% Plot the directivity pattern
% the title and graphical properties are irrelevant.
hp = plotResponse(h, F(1), PS, 'Unit','dbi', 'Format', 'Polar', 'RespCut', '3D');
% the plot is a surf plot. it's gridded data. triangulate it.
[F V] = surf2patch(hp.XData,hp.YData,hp.ZData,'triangles');
% modern versions since R2018b have a built-in STL encoder.
stlwrite(triangulation(F,V),'test.stl')
% read back the file so that we can prove it worked.
figure
T = stlread('test.stl');
trisurf(T,'facecolor',[1 1 1]*0.8,'edgecolor','none')
view(3); view(-30,47); camlight;
axis equal; grid on
See also:

请先登录,再进行评论。


Young Dae
Young Dae 2018-9-14
编辑:Young Dae 2018-9-14
  1 个评论
DGM
DGM 2025-9-27,1:50
That isn't applicable to gridded surf data or triangulated meshes. That's for volumetric images.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by