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

1 Ansicht (letzte 30 Tage)
EM geo
EM geo am 18 Mär. 2020
Beantwortet: dpb am 19 Mär. 2020
  3 Kommentare
EM geo
EM geo am 18 Mär. 2020
fileID = fopen('output_nostri.txt');
C = textscan(fileID,'%d-%d:%s %s');
fclose(fileID);
@Rik
Rik
Rik am 18 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 19 Mär. 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...

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by