execute a script in a loop

6 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2018-11-22
编辑:Stephen23 2021-4-26
"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!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by