Finding certain data in a data structure

Here is the full question: The attached data file ‘donors.dat’ stores blood donor information for a biomedical research company.
Open and read the data file and understand its format. For every donor, it stores the person’s name, blood pressure, blood type, and Rh factor information. The Blood type is either A, B, AB, or O. The Rh factor is + or -.
Write a function that takes one input argument: the blood type in the format of a string (A, B, AB, or O). The function will display the names of all donors with the same blood type as the input argument.
I need some ideas on how I can search the data structure to find those who share the same blood type, and print their names. This is what I have written so far:
function week8_3(blood)
[fid,msg]=fopen('donors.dat','r');
if fid==-1
disp(msg);
else
while ~feof(fid)
aline=textscan(fid,'%s %s %f %f %s %s');
[first,rest]=strtok(aline);
[last, rest]=strtok(rest(2:end));
[bloodtype,rest]=strtok(rest(4:end));
I have also attached the data file I am referencing.
Any help is appreciated!

8 Comments

File did not get attached
I could not attach my file due to it being a .dat file, so I inserted a picture of what the file looks like
You can rename dat files to txt or you can zip any file and attach the zip
I'm not sure how this will help me write a code for finding certain data with a data set?
If we had an actual copy of the data file, it would make it easier for us to provide advice on how to read the file.
However, I suspect that your textscan() is probably good enough.
I have already stated that I can not upload a .dat file, and I also do not know how to upload it as anything other than that.
At the MATLAB comand prompt:
zip donars.zip donors.dat
That will create a donars.zip file that you can upload here using the paperclip icon that is the second-last icon to the right (just to the left of the (?) HELP icon)
The file has been attached.

Sign in to comment.

Answers (1)

That form of textscan would read the entire file, with no need for a loop. The result would be a cell array with 6 entry. The first and second and fifth and sixth entries would each be a cell column array of character vectors. The third and fourth entry would each be a numeric array. There would be no need to strtok because the entries would already be separated.
strcmp(aline{5}, BloodType)
would return a logical vector indicating which rows match. The logical vector could be used to index aline{1} to get names.

5 Comments

That's very helpful. I rewrote my program using strcmp, but I have another question. Should I use an if statement like:
m=strcmp(aline{5}, blood)
if m==1
If I do, how should I go about displaying the information of those that match that bloodt ype?
mask = strcmp(aline{5}, blood);
names = strcat(aline{1}(mask), {' '}, aline{2}(mask));
This is not working for my code, I will have to try other options.
If we had a copy of the file, we could experiment...
Your file has a header line
First Last Systolic Diastolic BloodType Rh
When you do the textscan() you need to use 'HeaderLines', 1
Also when you use
[fid,msg]=fopen('donors.dat','r');
you should change the 'r' to 'rt' as there are carriage returns in the .dat file.

Sign in to comment.

Categories

Find more on Genomics and Next Generation Sequencing in Help Center and File Exchange

Asked:

on 23 Jul 2019

Commented:

on 26 Jul 2019

Community Treasure Hunt

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

Start Hunting!