Importing multiple files using a loop

15 Ansichten (letzte 30 Tage)
tjerk Verweij
tjerk Verweij am 28 Sep. 2023
Beantwortet: Image Analyst am 28 Sep. 2023
Hello, i need to import multiple files and instead of writing them all down, i want to use a for loop for it.
These are the file names that i need to import, with 1 changing number in the file names.
A1=importdata('012_2023ef400t.txt');
A2=importdata('013_2023ef400t.txt');
A3=importdata('014_2023ef400t.txt');
this is the beginning, but i need to import 30 files.
Any suggestions?
Thank you!

Akzeptierte Antwort

Dyuman Joshi
Dyuman Joshi am 28 Sep. 2023
Bearbeitet: Dyuman Joshi am 28 Sep. 2023
This is a general idea -
n=30;
%Preallocate
A = cell(1,n);
%Storing data
for k=1:n
A{k} = importdata(sprintf('0%d_2023ef400t.txt', k+11));
end
Use indices to access the data in the stored array.
Depending upon the data you have, you can preallocate double or strings array instead of cell array.
I would suggest to use readmatrix or readtable instead of importdata().
  2 Kommentare
tjerk Verweij
tjerk Verweij am 28 Sep. 2023
thanks!
apparently i had already a way to import the data correctly, i just did not look good enough for where the data was stored.
But my for loop was quite a bit bigger, so thanks for giving me a cleaner, smaller one :)
Dyuman Joshi
Dyuman Joshi am 28 Sep. 2023
You are welcome!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 28 Sep. 2023

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by