how to read and create column vectors from this txt file (attached)?

9 views (last 30 days)
  3 Comments
EM geo
EM geo on 18 Mar 2020
fileID = fopen('output_nostri.txt');
C = textscan(fileID,'%d-%d:%s %s');
fclose(fileID);
@Rik
Rik
Rik on 18 Mar 2020
Matlab by default treats the comma as a separator, not as part of a number. Since your columns seem to be alligned with fixed-width fields, it might make sense to parse the lines yourself by first reading the lines as char arrays, skipping the first 3 lines, replacing the commas by periods, and then using textscan on that. It might be easiest to read the date separately from the numeric values.

Sign in to comment.

Accepted Answer

dpb
dpb on 19 Mar 2020
A fixedWidthImportOptions object can help here altho the heading row doesn't quite line up to make it as trivial as could otherwise be...
opt=fixedWidthImportOptions('NumVariables',10); % create base import object w 10 variables
opt.VariableWidths=[8 10*ones(1,9)]; % set field width to match file
opt.DataLines=[4 inf]; % data starts row 4
tout=readtable('output_nostri.txt',opt); % import data; will be cellstr arrays
for i=2:10 % convert the numeric data
tout.(tout.Properties.VariableNames{i})=str2double(strrep(tout{:,i},',','.'));
end
tout.Var1=datetime(tout.Var1,'InputFormat','MMM-yyyy'); % and the time
I presume local settings will handle the month name abbreviations; the above fails on all that aren't english translations here; I would presume that comes from a locale setting for other places but I've never had the opportunity to test to know if that is so or not.
Also leaves variable names Var1 thru Var10; the column headings in line two don't exactly align with the data columns so to read it would have to do a second read of that line...

More Answers (0)

Community Treasure Hunt

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

Start Hunting!