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

9 次查看(过去 30 天)
  3 个评论
EM geo
EM geo 2020-3-18
fileID = fopen('output_nostri.txt');
C = textscan(fileID,'%d-%d:%s %s');
fclose(fileID);
@Rik
Rik
Rik 2020-3-18
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.

请先登录,再进行评论。

采纳的回答

dpb
dpb 2020-3-19
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...

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by