Code in loop won't execute

1 view (last 30 days)
I have a code in a for-loop that only partiallys executes. There's 9 lines (67-76) that only run once, at the end of the loop.
Please help me see what's wrong here?
mainDirectory='C:\Users\cs3761\Documents\MATLAB'; % folder with specimen folders
% mainDirectory='C:\Users\am6416\Documents\MATLAB\Help Charles'; % Equivalent main directory on my computer
listing = dir(mainDirectory); % get main directory info
nFolders=sum(horzcat(listing.isdir)); % create identifier for number of folders - this line simply is for a check and is not actually needed
for i=3:n
if listing(i).isdir == 1 % if the item is a folder - do stuff
sampleName=listing(i).name; % store a name for the sample
subDirectory=strcat(mainDirectory,'\',sampleName); % create string that is the file path to your folder
fullFilePath=strcat(subDirectory,'\','specimen.dat'); % this variable fullFilePath is used in the call to readtable instead of the generic 'specimen.dat' - specifies which specimen.dat
% Load data file and rename first 3 columns
T = readtable(fullFilePath,'NumHeaderLines',15); % Skip first 15 rows
T = renamevars(T,["Var1","Var2", "Var3"],["Time","Displacement", "Force"]); % Rename first three columns
% Calculate corrected torque, displacement and force
T.CorrDisplacement = (T.Displacement)/(cosd(0));
T.CorrForce = (T.Force)/(cosd(0));
CorrClamp_Radius = 51.5706; % Calculate this from specific sample clamp and cable
T.Angular_Rot = (T.CorrDisplacement*360)/(2*pi*CorrClamp_Radius);
T.Torque = (T.CorrForce)*(CorrClamp_Radius)/1000;
OverrunTorque = 0.25; % Overrun torque from standard AID in Nm
cf1 = 1.08; % Standard curve correction factor
cf2 = cf1 + OverrunTorque;
T.CorrTorque = (T.Torque)-cf2;
% plot hysteresis graph
figure; % creates new figure for each plot instead of overwriting axes
grid on % turn on major gridlines
grid minor % turn on minor gridlines
tit.Interpreter='none'; % turning off the text interpreter will prevent interpretation of underscores as subscript syntax
xlabel('Angular Rotation (deg)');
% Save the input as MATLAB figure
figureName = strcat(subDirectory, '.png');
saveas(gcf, figureName) % this will place the figure in the subDirectory folder
% Write to excel
writetable(T,strcat(mainDirectory,'\',spreadsheetName),'Sheet',sampleName,'WriteMode','append') % writes each table to separate sheet of output spreadsheet
% MATLAB 2018b equivalent below
% writetable(T,strcat(mainDirectory,'\',spreadsheetName),'Sheet',sampleName) % writes each table to separate sheet
% Export figure to excel
pos=get(gcf,'Position'); % get the figure location and size
% Calculate spring rate
a = T{125:125,{'CorrTorque'}};
b = T{44:44,{'CorrTorque'}};
c = T{125:125,{'Angular_Rot'}};
d = T{44:44,{'Angular_Rot'}};
Spring_Rate = (a-b)/(c-d);
SR = 'Spring Rate';
writematrix (Spring_Rate,spreadsheetName,'Range','M28:M28')
else % directory item is not a folder, keep moving
end % end if
end % end for
Charles Sendegeya
Charles Sendegeya on 10 Jan 2022
Thanks. Let me look into that.
The script was working well without issue until I added lines 67 to 76.

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 10 Jan 2022
Here, isdir is a field in the structure returned by dir, and not a use of the isdir function (I initially thought the same thing).
If none of the code in your if statement is running, then the condition of your if statement is never true.
I'm not sure the 9 lines only run once. I think the issue is they always write to the same spot in the same spreadsheet, so it may appear it only runs once. What appears in the Results.xlsx is the Spring_Rate of the final loop only.
BTW, you might be interested in this page on Accessing Data in Tables. You can define a,b,c and d this way:
% Calculate spring rate
a = T.CorrTorque(125);
b = T.CorrTorque(44);
c = T.Angular_Rot(125);
d = T.Angular_Rot(44);
  1 Comment
Charles Sendegeya
Charles Sendegeya on 10 Jan 2022
Thank you. Good observation. It is likely spring rate is being returned to the same cell and overwriting previous result.
Let me look into that.

Sign in to comment.

More Answers (0)





Community Treasure Hunt

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

Start Hunting!