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
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.
Golnar
Golnar 2014-3-23
I've changed the format to plain text, but the rest of the file remains the same. How do I parse strings, my friend?
And to extract three of the parameters (healthy_exposed, pus, necrotic) for the purpose of a stacked bar graph, the following code is not working:
figure;
bar(1:3, cat(1,[healthy_exposed, pus, necrotic]), 0.5, 'stack');
% Adjust the axis limits
axis([0 4 0 100]);
% Add title and axis labels
title('Chronology of Wound Specifications');
xlabel('Date of Visit');
ylabel('Percentage');
% Add a legend
legend('Healthy', 'Infection', 'Necrotic');

请先登录,再进行评论。

采纳的回答

per isakson
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

更多回答(1 个)

Walter Roberson
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.

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by