How to read in a file automatically through loop?
Ältere Kommentare anzeigen
Hello everyone!
I need your help in finding a way to read in a a file each iteration.
My Folder looks like that:

and I want that each time it reads the file in order.
For example, for j=1--> reads in the first file
for j=2--> reads in the second file
.....and so on.
Until now, I used to read in the files every time manually,but I would like if my script could do it on its own to save time.
I appreciate any help :)
12 Kommentare
Ted Shultz
am 22 Aug. 2019
Bearbeitet: Ted Shultz
am 22 Aug. 2019
fileList = ls ('*.mat')
you can then loop over the list you get.
Walter Roberson
am 22 Aug. 2019
You might want "natural file sort" from the File Exchange if you have more than 10 files, as otherwise Marker10 can end up being listed right after Marker1
Ifigenia Aslanidou
am 23 Aug. 2019
Stephen23
am 23 Aug. 2019
@Ifigenia Aslanidou : Note that ls is intended for displaying in the command window, it is not very practical for using within code. For processing filenames using code you should use dir, exactly as the MATLAB documenation recommends (check the examples):
You might like to check the many examples here:
Ifigenia Aslanidou
am 23 Aug. 2019
"How do I read in the files from it?"
As the dir documentation explains, that structure array contains the filenames (and other file meta-data), you can access the filenames using the name field, e.g. the k-th name is:
S(k).name
You will need to add the subfolder to that filename using fullfile. To actually import the file data you will need to pick a suitable function (for .mat files load is the function to use):
P = 'C:\Users\aslan\OneDrive\Desktop\Studienarbeit 2019\Messungen'; % Do NOT use "path" !
S = dir(fullfile(path,'*.mat')); % do NOT write the separator.
for k = 1:numel(S)
F = fullfile(P,S(k).name);
T = load(F);
... do whatever with the structure T
end
See also:
Ifigenia Aslanidou
am 23 Aug. 2019
"I get an error that says 'Undefined function or variable 'OUTPUT' '"
Without seeing your code I have no idea what you are doing. Possibly you need to access the output field of the structure that you load-ed.
T = load(...);
OUTPUT = T.OUTPUT;
If that does not resolve your issue, then you need to show the exact code that you are using, and also give the complete error message. That means all of the red text.
Walter Roberson
am 23 Aug. 2019
Also show the result of
whos('-file', 'C:\Users\aslan\OneDrive\Desktop\Studienarbeit 2019\Messungen\201804_A81)001_Marker1_30s_16*_943hPa_pp.mat')
(repair the file name if I made a typing mistake; the font is somewhat small and I am not certain I got all of the characters correct.)
Ifigenia Aslanidou
am 25 Aug. 2019
Ifigenia Aslanidou
am 25 Aug. 2019
Ifigenia Aslanidou
am 25 Aug. 2019
Antworten (0)
Kategorien
Mehr zu Variables finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

