フィルターのクリア

How to read a txt file with values and strings?

1 回表示 (過去 30 日間)
Alex
Alex 2017 年 3 月 18 日
コメント済み: Alex 2017 年 3 月 18 日
Each time I run a scenario, my script opens a file called changing.txt and adds a line with the important parameters and the filename for future statistical analysis. I want to open the file and have a variable for each column in the txt file. I will then look for duplicate scenario names and only analyze unique data for the statistical analysis. How do I do this?
%create a similar file to the one I'm working with
fileID=fopen('changing.txt','w');
fmt='%8.1f %8.4f %8.1f %8.1f %s\r\n';%format for fprint
fclose (fileID);
%write a few lines with duplicates
fileID=fopen('changing.txt','a');
fprintf(fileID,fmt, [ 1 2 3 4 'text.txt']);%write these values at the end of the file
fprintf(fileID,fmt, [ 4 3 2 1 'text2.txt']);%write these values at the end of the file
fprintf(fileID,fmt, [ 1 2 3 4 'text.txt']);%write these values at the end of the file
fprintf(fileID,fmt, [ 4 3 2 1 'text2.txt']);%write these values at the end of the file
fclose (fileID);
%
%open the file; one variable for each column in the text file
A = fscanf('changing.txt','%8.1f %8.4f %8.1f %8.1f %s\r\n') %(HELP HERE PLEASE)

採用された回答

per isakson
per isakson 2017 年 3 月 18 日
Replace
A = fscanf('changing.txt','%8.1f %8.4f %8.1f %8.1f %s\r\n');
by
fid = fopen( 'changing.txt', 'r' );
cac = textscan( fid, '%f%f%f%f%s', 'Collectoutput',true );
[~] = fclose( fid );
where changing.txt contains
1 2 3 4 text.txt
4 3 2 1 text2.txt
1 2 3 4 text.txt
4 3 2 1 text2.txt
check output
>> cac{1}
ans =
1 2 3 4
4 3 2 1
1 2 3 4
4 3 2 1
>> cac{2}
ans =
'text.txt'
'text2.txt'
'text.txt'
'text2.txt'
  1 件のコメント
Alex
Alex 2017 年 3 月 18 日
Excellent, thank you VERY much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by