How to generate multiple .stl files in a loop?

10 views (last 30 days)
My core question is: How to write multiple .stl files.
What I am doing here is: Open a .stl file (Code line 1), then rotate it in randomly generated axis (Code lines: 2 : 16) and finally write 10 .stl files.
When I run my codes, in command window it show 10 files succesfully written, but when I check in directory it shows only 1 file.
I think there is something wrong with the way I am try to use stlread (function) in for loop to write multiple .stl files.
[F,V,N] = stlread ('Directory_name:\Model_STL_Binary_0_0_0.stl'); %.stl file location
theta_x = randi ([0,180],10,1);
theta_y = randi ([0,180],10,1);
theta_z = randi ([0,180],10,1);
T = table(theta_x,theta_y,theta_z);
theta_angles =table2array(T);
ax = deg2rad(theta_angles(:,1));
ay = deg2rad(theta_angles(:,2));
az = deg2rad(theta_angles(:,3));
for i= 1:length(theta_angles)
Rx = [ 1, 0, 0 ; 0, cos(ax(i)), -sin(ax(i)); 0, sin(ax(i)), cos(ax(i))];
Ry = [ cos(ay(i)), 0, sin(ay(i)) ;0, 1, 0 ; -sin(ay(i)), 0, cos(ay(i)) ];
Rz = [ cos(az(i)), -sin(az(i)), 0 ; sin(az(i)), cos(az(i)), 0 ; 0, 0, 1 ];
pointR1 = V*Rx;
pointR2 = pointR1*Ry;
pointR3 = pointR2*Rz;
stlwrite('check.stl', F, pointR3(:,1:3));
end

Accepted Answer

KSSV
KSSV on 16 Sep 2021
You are overwriitng the stl files. Always you are using the same name to .stl file. You need to give different filename; check the below addition.
[F,V,N] = stlread ('Directory_name:\Model_STL_Binary_0_0_0.stl'); %.stl file location
theta_x = randi ([0,180],10,1);
theta_y = randi ([0,180],10,1);
theta_z = randi ([0,180],10,1);
T = table(theta_x,theta_y,theta_z);
theta_angles =table2array(T);
ax = deg2rad(theta_angles(:,1));
ay = deg2rad(theta_angles(:,2));
az = deg2rad(theta_angles(:,3));
for i= 1:length(theta_angles)
Rx = [ 1, 0, 0 ; 0, cos(ax(i)), -sin(ax(i)); 0, sin(ax(i)), cos(ax(i))];
Ry = [ cos(ay(i)), 0, sin(ay(i)) ;0, 1, 0 ; -sin(ay(i)), 0, cos(ay(i)) ];
Rz = [ cos(az(i)), -sin(az(i)), 0 ; sin(az(i)), cos(az(i)), 0 ; 0, 0, 1 ];
pointR1 = V*Rx;
pointR2 = pointR1*Ry;
pointR3 = pointR2*Rz;
filename = strcat('check_',num2str(i),'.stl') ; % File name based on loop index
stlwrite(filename, F, pointR3(:,1:3));
end
  1 Comment
Hasnain Raja Muhammad
Hasnain Raja Muhammad on 16 Sep 2021
My bad! You are right, I was overwriting the file with same file-name.
Thanks 👍🏻

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!