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!