How to read two columns from a text file that does not only consist of a table?

9 ビュー (過去 30 日間)
Hannah
Hannah 2021 年 8 月 12 日
コメント済み: Hannah 2021 年 8 月 12 日
I have a text file (see attachment) that I want to import into MATLAB. How can I only read/ store the columns 'month' and 'H-Gh'? i.e. I want my output to be:
Jan 33
Feb 52
Mar 97
Apr 123
May 161
Jun 178
Jul 176
Aug 153
Sep 105
Oct 63
Nov 33
Dec 24
so far I only managed to read one element from one column:
while ischar(tline) %returns 1 if tline is a character array and 0 otherwise.
colonLocation = strfind(tline, ['Sep']); %returns the starting indices of any occurrences of PATTERN in TEXT
if ~isempty(colonLocation)
subString = tline(:,[4:7])
output(k) = str2double(subString);
k = k + 1;
end
tline = fgetl(fid);
end
fclose(fid);

採用された回答

Walter Roberson
Walter Roberson 2021 年 8 月 12 日
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/709847/t1_r1_c1125oE-mon.txt';
T = readtable(filename, 'Headerlines', 12);
subset = T(1:12, {'Month', 'H_Gh'})
subset = 12×2 table
Month H_Gh _______ ____ {'Jan'} 33 {'Feb'} 52 {'Mar'} 97 {'Apr'} 123 {'May'} 161 {'Jun'} 178 {'Jul'} 176 {'Aug'} 153 {'Sep'} 105 {'Oct'} 63 {'Nov'} 33 {'Dec'} 24

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by