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

조회 수: 2 (최근 30일)
EM geo
EM geo 2020년 3월 18일
답변: dpb 2020년 3월 19일
  댓글 수: 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개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by