insert for loop in the given script

1 回表示 (過去 30 日間)
Federico Paolucci
Federico Paolucci 2022 年 9 月 7 日
回答済み: Vidhi Agarwal 2024 年 11 月 25 日
Hi, I have this script working for LN = [1], I need to modify it to make it work for LN 1 to n. I was thinking of using a for loop.
%%%%%%%%% estraggo il LAYER numero:[single or multiple]
LN=[1];
%cerco in III colonna i calori di LN
for i=1:Nrow
if isempty(WS{i,3})
idx(i,2)=0;
continue
end
if ismember(WS{i,3},LN)
idx(i,2)=1;
end
end
% %%Prendo solo i wall outer
% for i=1:Nrow
% if isempty(WS{i,5})
% idx(i,3)=0;
% continue
% end
% idx(i,3)=double(strcmp(WS{i,5},'WALL-OUTER'));
% end
%%Prendo TUTTE le linee depositate
for i=1:Nrow
if isempty(WS{i,5})
idx(i,3)=0;
else
idx(i,3)=1;
end
end
%%prendo solo quelli con deposition ==1
for i=1:Nrow
if isempty(WS{i,7})
idx(i,4)=0;
continue
end
idx(i,4)=WS{i,7};
end
idx=prod(idx,2);
figure(1)
axis equal
for i=1:Nrow
if idx(i)==0
continue
end
X=[WS{i,1}(1),WS{i,2}(1)];
Y=[WS{i,1}(2),WS{i,2}(2)];
Z=[WS{i,1}(3),WS{i,2}(3)];
plot3(X,Y,Z,'r');
hold on
end
xlabel('x,[mm]')
ylabel('y,[mm]')
zlabel('z,[mm]')
%%
Xws=[];
Yws=[];
Xmin=75;
Xmax=105;
Ymin=60;
Ymax=90;
Res=0.01;
Xmesh=[Xmin:Res:Xmax];
Ymesh=[Ymin:Res:Ymax];
[Xmesh,Ymesh]=meshgrid(Xmesh,Ymesh);
Xvmesh=reshape(Xmesh,numel(Xmesh),1);
Yvmesh=reshape(Ymesh,numel(Ymesh),1);
XYvmesh=[Xvmesh,Yvmesh];
Amatrix=zeros(size(Xmesh));
for i=1:Nrow
disp(i/Nrow*100);
if idx(i)==0
continue
end
X=[WS{i,1}(1),WS{i,2}(1)];
Y=[WS{i,1}(2),WS{i,2}(2)];
L=((X(2)-X(1))^2+(Y(2)-Y(1))^2)^0.5;
if L>Res
stp=ceil(L/Res);
u=[0:1/stp:1];
xp=X(1)+(X(2)-X(1))*u;
yp=Y(1)+(Y(2)-Y(1))*u;
Xws=[Xws,xp];
Yws=[Yws,yp];
end
end
pause(0.5)
clc
Points=unique((round(1/Res*[Xws',Yws']))*Res,'rows');
figure(2)
hold on
xlim([Xmin Xmax]);
ylim([Ymin, Ymax]);
axis equal
plot(Points(:,1),Points(:,2),'r.')
x=(Points(:,1)-Xmin)/Res;
y=(Points(:,2)-Ymin)/Res;
for i=1:numel(x)
disp(i)
Amatrix(uint64(y(i)),uint64(x(i)))=1;
end
%morphological operation
SE = strel("disk",round((0.35/2)/Res));
Adil = imdilate(Amatrix,SE);
figure(3)
surf(Xmesh,Ymesh,Adil,'linestyle','none')
axis equal
view(2)

回答 (1 件)

Vidhi Agarwal
Vidhi Agarwal 2024 年 11 月 25 日
Using a for loop to traverse over each layer number could be useful when running the script for LNs ranging from 1 to n. The goal is to modify the logic to manage more than one layer rather than simply one. The following steps may assist you in getting started:
  1. Replace the single value LN = [1]; with a range, like LN = 1:n;.
  2. Use a loop to iterate over each layer number and apply your logic.
  3. Accumulate results for each layer if needed.
Revised version of the code is given below:
n = 5; % For example, if you want to iterate from 1 to 5
LN = 1:n;
idx = zeros(Nrow, 4); % Initialize idx matrix
% Iterate over each layer number
for layer = LN
% Find indices for the current layer
for i = 1:Nrow
if isempty(WS{i,3})
idx(i,2) = 0;
continue;
end
if WS{i,3} == layer
idx(i,2) = 1;
end
end
for i = 1:Nrow
if isempty(WS{i,5})
idx(i,3) = 0;
else
idx(i,3) = 1;
end
end
for i = 1:Nrow
if isempty(WS{i,7})
idx(i,4) = 0;
continue;
end
idx(i,4) = WS{i,7};
end
idx_product = prod(idx, 2);
% Plotting for the current layer
figure(1)
axis equal
for i = 1:Nrow
if idx_product(i) == 0
continue;
end
X = [WS{i,1}(1), WS{i,2}(1)];
Y = [WS{i,1}(2), WS{i,2}(2)];
Z = [WS{i,1}(3), WS{i,2}(3)];
plot3(X, Y, Z, 'r');
hold on
end
end
xlabel('x,[mm]')
ylabel('y,[mm]')
zlabel('z,[mm]')
% The rest of your code remains unchanged
Hope that helps!

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by