Finding certain data in a data structure

1 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2019-7-24
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)

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2019-7-24
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 个评论
Walter Roberson
Walter Roberson 2019-7-24
If we had a copy of the file, we could experiment...
Walter Roberson
Walter Roberson 2019-7-26
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Phylogenetic Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by