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:
- Replace the single value LN = [1]; with a range, like LN = 1:n;.
- Use a loop to iterate over each layer number and apply your logic.
- 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!