for loop to read different files

조회 수: 15 (최근 30일)
Gemma
Gemma 2023년 1월 20일
답변: Sulaymon Eshkabilov 2023년 1월 21일
I'm trying to run a for loop to read 3 different files loaded already on my workspace. Add the values on file 1 to file 2. Extract from another file the data and add +600, add that new data to an array and plot it. Any help with the code for this is really appreciated!
Something like this:
for j=1 numFiles
***read file 1
***read file 2
***add file 1 to file 2
***extract file 3 (new data +600)
*** add that chunk of data to an array
end
plot new chunk of data

답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 1월 21일
There are a few different ways that can do this exercise. Here is one of them:
clearvars
FILE = fullfile('C:\Users\...', '*.txt'); % Directory where the files are residing. Your data file extension, e.g.: .txt, .xlsx, .csv, etc
LIST = dir(FILE);
N = length(LIST); % N = data files
D = zeros(1e3, 1e3, N);
for ii = 1 : N
FFName = fullfile(LIST(ii).folder, LIST(ii).name);
DATA = readmatrix(FFName);
D(:, :, ii) = DATA; % NOW D contains all data from N files
end
plot() % Select and process the part of D that is necessary
You can adjust this shown loop code for three different files

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by