Question about fgetl(fileID)

4 visualizzazioni (ultimi 30 giorni)
Golnar
Golnar il 22 Mar 2014
Risposto: Walter Roberson il 23 Mar 2014
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 Commenti
dpb
dpb il 22 Mar 2014
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 il 23 Mar 2014
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');

Accedi per commentare.

Risposta accettata

per isakson
per isakson il 23 Mar 2014
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

Più risposte (1)

Walter Roberson
Walter Roberson il 23 Mar 2014
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.

Categorie

Scopri di più su Text Data Preparation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by