Code in loop won't execute

2 visualizaciones (últimos 30 días)
Charles Sendegeya
Charles Sendegeya el 10 de En. de 2022
Comentada: Charles Sendegeya el 10 de En. de 2022
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 comentarios
David Hernandez
David Hernandez el 10 de En. de 2022
Editada: David Hernandez el 10 de En. de 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
Charles Sendegeya el 10 de En. de 2022
Thanks. Let me look into that.
The script was working well without issue until I added lines 67 to 76.

Iniciar sesión para comentar.

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 10 de En. de 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 comentario
Charles Sendegeya
Charles Sendegeya el 10 de En. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by