Code in loop won't execute
Show older comments
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?
spreadsheetName='Results.xlsx';
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
n=numel(listing);
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
plot(T.Angular_Rot,T.CorrTorque);
tit=title(sampleName);
tit.Interpreter='none'; % turning off the text interpreter will prevent interpretation of underscores as subscript syntax
xlabel('Angular Rotation (deg)');
ylabel('Torque(Nm)');
% 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
xlsPasteTo(spreadsheetName,sampleName,pos(3),pos(4),'J2');
% 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(SR,spreadsheetName,'Range','L28:L28')
writematrix (Spring_Rate,spreadsheetName,'Range','M28:M28')
else % directory item is not a folder, keep moving
end % end if
end % end for
2 Comments
David Hernandez
on 10 Jan 2022
Edited: David Hernandez
on 10 Jan 2022
Not sure if you are using the right syntax for the line 9 (isdir)
Also Mathworks recommend to use 'isfolder' instead, I gues using this function can fix it.
https://www.mathworks.com/help/matlab/ref/isfolder.html
Charles Sendegeya
on 10 Jan 2022
Accepted Answer
More Answers (0)
Categories
Find more on Scripts in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!