필터 지우기
필터 지우기

execute a script in a loop

조회 수: 6 (최근 30일)
Ahmed Alsaadi
Ahmed Alsaadi 2018년 11월 21일
편집: Stephen23 2021년 4월 26일
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!
  댓글 수: 1
Ahmed Alsaadi
Ahmed Alsaadi 2018년 11월 22일
Both work well, thank you very much.

댓글을 달려면 로그인하십시오.

추가 답변 (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