Question about fgetl(fileID)
4 ビュー (過去 30 日間)
古いコメントを表示
I have a file named report_data.rtf which looks like
I want my code to grab these, and insert them somewhere. I've used:
fileID = fopen('report_data.rtf','r');
patientName = fgetl(fileID);
dateOfBirth = fgetl(fileID);
healthy_exposed = fgetl(fileID);
pus = fgetl(fileID);
necrotic = fgetl(fileID);
ulcer_stage = fgetl(fileID);
area = fgetl(fileID);
volume = fgetl(fileID);
fclose(fileID);
But I'm getting an error grabbing the values. How should my report_data.rtf file look like so that my code can grab them properly?
2 件のコメント
dpb
2014 年 3 月 22 日
Save the file as plain text, not .rtf (rich text format) which has a bunch of formatting info in it besides the actual data.
Then, presuming you want to actually read the values of the variables not just the whole text line, you'll need to parse the data. The patient name if it isn't delimited will be the fly in the ointment as the first and last name will be parsed as two separate strings unless enclosed in quotes to indicate it's a single string with embedded space.
採用された回答
per isakson
2014 年 3 月 23 日
Try
function ccsm()
fid = fopen( 'cssm.txt', 'r' );
cac = regexp( fgetl( fid ), '=', 'split' );
patieritName = cac{2};
cac = regexp( fgetl( fid ), '=', 'split' );
dataofBwth = datenum( cac{2}, 'mm/ddyyyy' );
cac = regexp( fgetl( fid ), '=', 'split' );
heaFthy_exposed = str2double(cac{2});
cac = regexp( fgetl( fid ), '=', 'split' );
pus = str2double(cac{2});
cac = regexp( fgetl( fid ), '=', 'split' );
necrotic = str2double(cac{2});
cac = regexp( fgetl( fid ), '=', 'split' );
ulcer_stage = str2double(cac{2});
cac = regexp( fgetl( fid ), '=', 'split' );
area = str2double(cac{2});
cac = regexp( fgetl( fid ), '=', 'split' );
volume = str2double(cac{2});
fclose('all');
end
where cssm.txt contains
patieritName = John Doe
dataofBwth = 04/2511987
heaFthy_exposed =75
pus = 10
necrotic = 15
ulcer_stage = 3
area = 89
volume = 90
0 件のコメント
その他の回答 (1 件)
Walter Roberson
2014 年 3 月 23 日
fgetl() retrieves entire lines as strings. You need to extract the numeric information.
You should consider using fileread() to read the entire text file and then using regexp() to extract the strings corresponding to the numbers of interest, followed by str2double() to convert them to numeric form.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Text Data Preparation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!