for loop to read different files

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