For Loop Not Executing Properly
Show older comments
I have two for loops in my code that is supposed to cycle through a number of files, extract the data, plot it, and then save the plots as .png files, however it always skips to the last file in the sequence and runs the code for just that.
Can someone tell me where I'm going wrong in my code?
FMdir = dir('*FM*.ep'); %Get a list of all of the final measurement files in the directory
FMn = numel(FMdir); %cast as a double
InMdir = dir('*InM*.ep'); %Get a list of all the initial measurement files in the directory
InMn = numel(InMdir); %cast as a double
if (FMn ~= InMn) % check to see if the number of final measurement files matches the number of initial measurement files
error('Insufficient number of file pairs, please check naming and try again') %if they don't... throw an erorr
end
%%first for loop
for n= 1:FMn %cycle through
finalname = FMdir(n).name;
end
start = 1;
out = n;
%% second for loop
for i = start:out
num_strfinal = num2str(i, '%02.f');
number = string(num_strfinal);
%%load files
FM = dir('C:\Users\n\Desktop\F-DAS Matlab\ProcessFiles\*FM'+ number + '_*.ep');
FinalM = getfield(FM, 'name');
IN = dir('C:\Users\n\Desktop\F-DAS Matlab\ProcessFiles\*InM'+ number + '_*.ep');
InitialM = getfield(IN, 'name');
%%import data
opts = detectImportOptions(FinalM, 'FileType','text');
opts.DataLines = [1, Inf];
opts.Delimiter = ",";
opts.VariableNames = ["sam12mark", "dashcolorforcolumn2"];
opts.VariableTypes = ["double", "double"];
opts.ImportErrorRule = "omitrow";
opts.MissingRule = "omitrow";
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
Final = readtable(FinalM, opts);
opts = detectImportOptions(InitialM, 'FileType','text');
opts.DataLines = [1, Inf];
opts.Delimiter = ",";
opts.VariableNames = ["sam12mark", "dashcolorforcolumn2"];
opts.VariableTypes = ["double", "double"];
opts.ImportErrorRule = "omitrow";
opts.MissingRule = "omitrow";
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
Initial = readtable(InitialM, opts);
%% Determine difference between final and initial measurements
Difference = Final{:,2} - Initial{:,2};
%%Plot final, initial, and difference on a semilog plot
subplot (2,1,1)
semilogx(Initial{:,1},Initial{:,2})
title('Final & Initial Measurements')
xlabel ('Frequency'); ylabel('Magnitude')
hold on
semilogx(Final{:,1},Final{:,2})
xlabel ('Frequency (Hz)'); ylabel('Magnitude (dB)');
legend('InM','FM');
hold off
subplot (2,1,2)
semilogx(Initial{:,1},Difference(:,1))
title('Calculated Difference')
xlabel ('Frequency (Hz)'); ylabel('Magntiude (dB)')
legend('FM', 'InM')
saveas(gcf, 'figure.fig' )
saveas(gcf, 'figure.png')
%make directory to move figures to
if ~exist('C:\Users\n\Desktop\F-DAS Matlab\Figures', 'dir')
mkdir('C:\Users\n\Desktop\F-DAS Matlab\','Figures');
end
%save and move figures
movefile *.fig 'C:\Users\n\Desktop\F-DAS Matlab\Figures'
movefile *.png 'C:\Users\n\Desktop\F-DAS Matlab\Figures'
end
Sorry for the block of code, I didn't know how much to include.
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!