Question about fgetl(fileID)
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Golnar
le 22 Mar 2014
Réponse apportée : Walter Roberson
le 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 commentaires
dpb
le 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.
Réponse acceptée
per isakson
le 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
0 commentaires
Plus de réponses (1)
Walter Roberson
le 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.
0 commentaires
Voir également
Catégories
En savoir plus sur Text Data Preparation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!