How do I ignore characters in a text file

33 ビュー (過去 30 日間)
Cole Rasmussen
Cole Rasmussen 2020 年 4 月 3 日
回答済み: Geoff Hayes 2020 年 4 月 3 日
So I have this file, (linked above) given to me and I need...
  • To put the first two integers into one matrix named "date" with two columns
  • Ignore the two character columns
  • Put the other next two set of numbers in another matrix consisting of two columns
  • And then I need to put the next four set of numbers into a matrix consisting of four columns
  • And I can only put them into matrixes using the textread() function, not textscan or anything else
So the problem that I face is that I don't know how to ignore the character strings when taking into account the rest of the file.
So one of the closest attempts so far was when I did this code:
FILENAME = 'AU_SB_2020_08.txt';
[fid, msg] = fopen(FILENAME, 'r');
if fid <= 0
fprintf( 'File not available\n' )
else
% file exist continue
[month, day, place, name, score1, score2, s1, s2, s3, s4] = textread(...
FILENAME, '%f %f %s %s %f %f %f %f %f %f');
fclose(fid);
end
but obviously that saves the two character columns instead of ignoring them, so that didn't work.
I also feel like I got close when I did this code (just trying to get the date into one matrix for right now):
FILENAME = 'AU_SB_2020_08.txt';
[fid, msg] = fopen(FILENAME, 'r');
if fid <= 0
fprintf( 'File not available\n' )
else
% file exist continue
[date] = textread(FILENAME, '%f')
fclose(fid);
end
but for that one it says "Trouble reading floating point number from file (row 3, field 1) ==>" which I think is because there's a random extra whitespace in the file.
I've tried lots of researching and going over notes and I can't seem to find out how to fix this, so that's why I turned over to the internet.

回答 (1 件)

Geoff Hayes
Geoff Hayes 2020 年 4 月 3 日
Cole - since this is homework, we can only give out hints...so there is an example at textread that describes how to ignore a specific column in the file. You still need to acknowledge it in the format string but there is a way to indicate (within that string) how to ignore the column. As for putting the first two columns of data (from the file) into one matrix, consider how to - if given a matrix - access the first and second columns: A(:,1) and (A:,2)...

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by