How to input text file into matlab as an array
Show older comments
I have a large set of text files all of the same format. I need only the numerical data from these to be imported into matlab as an array, or as any other form so long as i can use it in my code.
I have attached an example of one of the files '15048.txt'.
Thanks
Answers (1)
Guillaume
on 29 Nov 2018
Probably, the easiest:
opts = detectImportOptions('15048.txt'); %let matlab figure out most of the file format
opts.ImportErrorRule = 'omitrow'; %so that it ignores the last line
opts.ExtraColumnsRule = 'ignore'; %otherwise, it detects an extra variable that is not there
opts.VariableNames(2:5) = {'PRECIP', 'EVAP', 'TMIN', 'TMAX'}; %matlab can't reliably detect the variable names as they're split over two rows
data = readtable('15048.txt', opts)
Categories
Find more on Text Data Preparation 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!