For Loop Not Executing Properly

1 visualización (últimos 30 días)
Nat Person
Nat Person el 5 de En. de 2021
Respondida: Cris LaPierre el 5 de En. de 2021
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.

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 5 de En. de 2021
It appears to me your code should be looping through all your files. How long does it take for your code to run?
A couple things that might be causing the issue.
  • You do not use a figure command, so all the plots in one loop replace the plots created in the previous loop, so at the end, you will only see the plots from the final loop displayed in MATLAB.
  • You save the figures using the same file names in each loop (figure.fig and figure.png), so each loop replaces the files created in the previous loop.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by