for loop to read different files

12 views (last 30 days)
Gemma
Gemma on 20 Jan 2023
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

Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 21 Jan 2023
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

Categories

Find more on Environment and Settings 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!