execute a script in a loop

8 views (last 30 days)
Ahmed Alsaadi
Ahmed Alsaadi on 21 Nov 2018
Edited: Stephen23 on 26 Apr 2021
I have made a script that calculates FFT, STFT, Wigner-Ville, etc. The inputs are file1.csv, file2.csv,file3.csv, etc. The script executes the inputs and gives me outputs, the outputs are crossponding to the input i.e output1, output2, output3, etc.
I want to create a loop that import the input files (file1.csv, file2.csv, etc) and executes them consecutively and save the output in a matrix, each column in the matrix represents an output i.e. column1 is output1 and colum2 is output 2 etc.

Accepted Answer

Stephen23
Stephen23 on 22 Nov 2018
Edited: Stephen23 on 26 Apr 2021
"The script executes the inputs and gives me outputs, the outputs are crossponding to the input i.e output1, output2, output3, etc."
Magically defining/accessing variable names is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know why:
Basically your bad data design willl force you into writing pointlessly slow, complex, and buggy code, but you can easily avoid this by designing your data better: do NOT have lots of separate numbered variables!
You can simply collect all of the data into one array (e.g. a cell array) inside the loop, for example:
N = 20;
C = cell(1,N);
for k = 1:N
output = ... your code
C{K} = output;
end
M = [C{:}];
plot(M)
You can easily loop over those files too, e.g.:
S = dir('*.csv');
S = natsortfiles(S); % download from FEX.
for k = 1:numel(S)
S(k).data = csvread(S(k).name); % use filename
end
M = [S.data]
This will be much simpler, more effiicent, and easier to debug than any code you could write with magically accessed variable names!

More Answers (0)

Categories

Find more on Audio I/O and Waveform Generation 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!