Using fscanf to correctly store them in a struct and go to the next patient

6 visualizaciones (últimos 30 días)
I am trying to read a file using fscanf with a while loop: 1) name 2) surname 3) age 4) unit 5) Random_Numer 6)Doctors my problem is how can i save them in structure using fscanf (example: patient(i).doctor) so it stops reading when it starts to read the new patient and stores it for example (day 1=doctor1 doctor2). ------------------------------------------------------------ 1)John 2)Noah 3)40 4)clinic1 5)8 6)(day1)Doctor1 \t 6)(day1)Doctor2 \t 6)(day1)Doctor3 \n 6)(day2)Doctor4 \t 6)(day2)Doctor5 \n
  24 comentarios
STYLIANOS CHRISTOFOROU
STYLIANOS CHRISTOFOROU el 7 de Jun. de 2022
First day of visit with doc1 \t doc2\n Second day of visit with doc1 \t doc2 \t doc3 \t doc4\n
dpb
dpb el 7 de Jun. de 2022
Which doesn't address the Q? about the correlation between ID and number at all...and, it's all immaterial anyway, as Walter shows, there's no way to parse the file unequivically from the content and the provided rules by which to do so.

Iniciar sesión para comentar.

Respuestas (2)

dpb
dpb el 5 de Jun. de 2022
Editada: dpb el 5 de Jun. de 2022
My approach would begin something like
P=readlines('Patients_Data.txt'); % read the file as string array
ixNumbers=find(matches(P,digitsPattern)); % find the numeric records -- age, ID
nPatients=numel(ixNumbers)/2; % there are two numeric records/patient
for i=1:nPatients % so, for each patient set
j=2*i-1; % skip every other age for indexing
S(i).Patient=P(ixNumbers(j)-1);
S(i).Age=str2double(P(ixNumbers(j)));
S(i).ID=str2double(P(ixNumbers(j)+2));
S(i).Clinic=P(ixNumbers(j)+1);
end
That gives the patient info; the doctors/days can be read from the section after the ID before the next patient; left as "exercise for student".
For the above, this yielded--
>> S(1)
ans =
struct with fields:
Patient: "John Noah"
Age: 40
ID: 8
Clinic: "clinic1"
>> S(2)
ans =
struct with fields:
Patient: "Oliver Alexander"
Age: 50
ID: 10
Clinic: "clinic2"
>>
In my opinion, that's a lot simpler than trying to parse the line record-by-record.
It's going to be VERY fragile in practice; but so will the attempt the other way; it's just a form rife for error in entry or parsing.

Walter Roberson
Walter Roberson el 6 de Jun. de 2022
That file format cannot be parsed. Unlike what you indicated when I asked, there is no tab on the line for the solo doctor Doctor7 and therefore you cannot reliably tell the difference between a single doctor visit and a patient name.
This ambiguity could be resolved if it could be guaranteed that patient names always had at least one embedded space, but that doctor names do not have any spaces.
Note that there are some countries where people only use a single name, and do not have a surname, not even for legal purposes (as far as I can tell).
  4 comentarios
STYLIANOS CHRISTOFOROU
STYLIANOS CHRISTOFOROU el 7 de Jun. de 2022
Ignore the arrows think of it like (day1)Doctor1\t(day1)Doctor2\n (day2)Doctor4\t(day2)Doctor5\n
We don't know how many lines there will be neither the amount of doctors per tab.
dpb
dpb el 7 de Jun. de 2022
There isn't a single \t character in the file first off...forget that idea.
As shown above, you also can't split() the fields without special processing to handle the screwed-up form in which there aren't the same number of delimiters per record.
As I showed in the Answer, the only way to parse this file is to make the assumptions that there are the two numeric fields for each patient and then infer the content of the other records based on their position relative to those.
As I showed (and left as "exercise for Student" since this is, after all, apparently a homework/class assignment), you can determine from those positions the number of records that are in the file between the present patient ID record and the next patient AGE record -- since it is presumed there's only one patient record, then one can also find out how many doctor/day records there are for the present patient and simply save what is there for each patient; dealing with the inconsistent number of delimiters however one chooses.
The outline is there; but unless you make gross assumption and just count records, it is simply not possible to decode this file from content alone.
End of story.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by