MATLAB Answers

For loop only completing once

2 views (last 30 days)
I had my code working perfectly then changed some things and now the looping only goes through once. basically im creating txt files and wanting to filter the names through so AL general it then puts in my format spec info (which i shortened for thi) and creates the next one. then AL bar and so forth and jumps to the next.
before it did exactly that then i changed some rows and now it only compleets one loop making the AL general and nothing more.
any ideas why?
%%trial al 6061%%
clc
clear workspace
baseName = ["AL-6061"; "test"; "try";"woo"]%;"see"];
Name = baseName;
Type=["general","bar", "rod","sheet"]%,"plate"]; %"plate"
%inside the code
FN=["Alumninum 6061";"testing";"tial";"woop"]%;"see"];
FullName=FN;
PTCCondtion =["AL_6061-T6";"con1";"con2";"con3"];%;"con4"];
DA= ["ref_color1";"color1";"color2";"color3"]%;"color4"]; %Default Appearance
%all info (pulled from excel numbers should be in order of general, bar, rod, sheet, plate)
%current layout says that all the values bellow for each section corospond
%with row values = the row name in base name then the indivigual columbs
%values = the colum in types so a Al general has a value of 1 for temp
%while a Al bar has a value of 2 and a test general is 6 test bar is 7
T=[1,2,3,4,5;6,7,8,9,10;11,12,13,14,15;16,17,18,19,20];%Temp
%info for for loop
[nrows,ncols] = size(baseName);
[nrows1,ncols1] = size(Type);
%DO NOT CHANGE (Darci this note is for when we have the final product
%though the code works right now so you shouldnt have to change it)
for k = 1:ncols
% build filename % e.g. AL083Bar.txt
for i=1:nrows1
filename = Name(i,1)+Type(1,k)+'.mtl';
f= fopen(filename,'w');
%code for the text in the file DO NOT CHANGE
formatSpec = 'ND_RelParSet_K01={'
fprintf(f,formatSpec,Name(i,k),FullName(i,k),T(i,k))%BF(i,k),TY(i,k),Y(i,k),PR(i,k),SM(i,k),MD(i,k),TE(i,k),SLT(i,k),SLC(i,k),SLS(i,k),SH(i,k),PTCC(i,1),PTCxh(i,1),HA(i,k),HT(i,k),TC(i,k),TYS(i,k),DA(i,1),TUS(i,k));
% write data to file
% writematrix(Formatspec,filename)
end
end
fclose('all')

Accepted Answer

Image Analyst
Image Analyst on 16 Sep 2021
ncol is 1. Use numel() instead of size():
%%trial al 6061%%
clc
clear workspace
baseName = ["AL-6061"; "test"; "try";"woo"]%;"see"];
Name = baseName;
Type=["general","bar", "rod","sheet"]%,"plate"]; %"plate"
%inside the code
FN=["Alumninum 6061";"testing";"tial";"woop"]%;"see"];
FullName=FN;
PTCCondtion =["AL_6061-T6";"con1";"con2";"con3"];%;"con4"];
DA= ["ref_color1";"color1";"color2";"color3"]%;"color4"]; %Default Appearance
%all info (pulled from excel numbers should be in order of general, bar, rod, sheet, plate)
%current layout says that all the values bellow for each section corospond
%with row values = the row name in base name then the indivigual columbs
%values = the colum in types so a Al general has a value of 1 for temp
%while a Al bar has a value of 2 and a test general is 6 test bar is 7
T=[1,2,3,4,5;6,7,8,9,10;11,12,13,14,15;16,17,18,19,20];%Temp
%info for for loop
numFiles = numel(baseName)
numTypes = numel(Type)
%DO NOT CHANGE (Darci this note is for when we have the final product
%though the code works right now so you shouldnt have to change it)
for k = 1 : numTypes
for i = 1 : numFiles
% Build filename % e.g. AL083Bar.txt
filename = Name(i,1)+Type(1,k)+'.mtl';
fprintf('Opening %s for writing.\n', filename);
fid = fopen(filename,'w');
%code for the text in the file DO NOT CHANGE
formatSpec = 'ND_RelParSet_K01={'
fprintf(fid,formatSpec,Name(i,k),FullName(i,k),T(i,k))%BF(i,k),TY(i,k),Y(i,k),PR(i,k),SM(i,k),MD(i,k),TE(i,k),SLT(i,k),SLC(i,k),SLS(i,k),SH(i,k),PTCC(i,1),PTCxh(i,1),HA(i,k),HT(i,k),TC(i,k),TYS(i,k),DA(i,1),TUS(i,k));
% write data to file
% writematrix(Formatspec,filename)
fclose(fid);
end
end

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by