Reading Multiple script files containing numbers and text, file format is not any specific format
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Mohammed Yousuf
am 28 Aug. 2019
Bearbeitet: meghannmarie
am 29 Aug. 2019
I have 365 files each day one file so the data is of one year. File is not of any fixed format like .dat or .xls or any other fixed format.
I can import it using import option one by one, But i would like to import or read them at one go all together in a loop which is creating a problem.
Each file contains six columns, separated with a space between them as shown below
473342400 17.41726119594118 78.55087294003569 441.657340252 0.0350254898955 HYDE
473342430 17.4172611595746 78.55087293204093 441.678895976 0.0350153127795 HYDE
473342460 17.417261172776456 78.55087291512065 441.682120113 0.0350026635609 HYDE
473342490 17.417261181614183 78.55087288613484 441.675300984 0.0349871066611 HYDE
473342520 17.417261194465333 78.55087289974891 441.676861182 0.0349686525377 HYDE
I want to read these files at one go.
Please help me.
0 Kommentare
Akzeptierte Antwort
meghannmarie
am 29 Aug. 2019
Bearbeitet: meghannmarie
am 29 Aug. 2019
If you have 2014a then you dont have the datastore function, you should upgrade to a newer version if you can. Otherwise, here is how todo it in a loop:
Folder = 'D:\files'; % Your folder with data files
FileList = dir(fullfile(Folder, '*'));
FileList(ismember({FileList.name}, {'.', '..'})) = []; % Remove . and ..
%% Setup the Import Options
opts = delimitedTextImportOptions("NumVariables", 6);
% Specify range and delimiter
opts.DataLines = [1, Inf];
opts.Delimiter = " ";
% Specify column names and types
opts.VariableNames = ["Var1", "Var2", "Var3", "Var4", "Var5", "Var6"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "char"];
opts = setvaropts(opts, 6, "EmptyFieldRule", "auto");
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
opts.LeadingDelimitersRule = "ignore";
data = [];
for f = 1:numel(FileList)
file = fullfile(FileList(f).folder,FileList(f).name);
data = cat(1,data,readtable(file, opts));
end
0 Kommentare
Weitere Antworten (1)
meghannmarie
am 28 Aug. 2019
Have you tried a datastore?
folder = 'D:\files'; %this is folder where the data files are
ds = datastore(folder);
data = readall(ds);
3 Kommentare
meghannmarie
am 28 Aug. 2019
Bearbeitet: meghannmarie
am 28 Aug. 2019
What version of MATLAB do you have? You need at least R2014b.
Siehe auch
Kategorien
Mehr zu Text Files 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!