フィルターのクリア

How can I read a specified, comma seperated text (txt) file line-byline?

1 回表示 (過去 30 日間)
Kalen Timbs
Kalen Timbs 2016 年 5 月 19 日
回答済み: JohnGalt 2016 年 5 月 19 日
I need to just read the 3 following numerical value from a text file using sscanf line by line:
0.3616813421 V,0 counts,500 ms
0.3567937374 V,0 counts,500 ms
0.3616813421 V,0 counts,500 ms
0.3616813421 V,0 counts,500 ms
0.3665689229 V,0 counts,500 ms
.
So far i have:
fileID=fopen('data.txt','rt');
line=fgetl(fileID);
a = fscanf('line','%s %s %s');
But this doesnt seem to work and im unsure as what to do.

採用された回答

JohnGalt
JohnGalt 2016 年 5 月 19 日
Now, you say that you'd like the 3 numerical values which can be obtained by:
fileID=fopen('data.txt','r'); % I've removed the 't'
while feof(fileID)~=1
line=fgetl(fileID); % as per OP
a = cell2mat((textscan(line,'%f V,%f counts,%f ms'))); % a is a 3x1 matrix
display(a)
% do something with 'a'
end
fclose(fileID);
However, if you wanted to read the file in one go... you could use:
fileID=fopen('data.txt','r'); % I've removed the 't'
a = cell2mat(textscan(fileID,'%f V,%f counts,%f ms'));
fclose(fileID);
display(a)
There are further improvements that can be made to this code (checking that the file opens correctly, using more appropriate data types etc etc) but this should achieve what you intend.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by