Extract and code gender string as number using if loop

I have data for 200 participants stored in a 1x1 structure with 3 fields (time data, gender and age). I'm trying to include lines of script that will extract the gender of each participant and code their gender as 0 (non-binary), 1 (female), or 2 (male). I'm also assigning an ID and extracting age, code so far is below:
for i = 1:size(files);
id = str2num(files(i).name(7:size(files(i).name,2)-4));
tempfile = load(files(i).name);
age = tempfile.data.age;
end
I don't have experience in using an if-loop on strings and am struggling to find the best command to use. I have tried the below, as this is what I would do if extracting and coding numbers, but obviously this doesn't work for these strings as they are not matched in size. I've also played around with STRCMP and STRNCMP but with no luck.
if tempfile.data.gender == 'non-binary'
gender = 0;
elseif tempfile.data.gender == 'female'
gender = 1;
elseif tempfile.data.gender == 'male'
gender = 2;
A little help would be much appreciated!

 채택된 답변

Star Strider
Star Strider 2019년 11월 20일

0 개 추천

I do not udnerstand the reason strcmp or strncmp would not work with your structure data. We may have to see ‘tempfile.data.gender’ in order to understand the problem.
Meanwhile, consider strcmpi to do case-insensitive comparisons.

댓글 수: 5

Jen
Jen 2019년 11월 20일
It isn't that strcmp or strncmp didn't work I just couldn't figure out how to format the command properly. I tried to use online guidance and the help function but I couldn't wrap my head around it.
Bob Thompson
Bob Thompson 2019년 11월 20일
편집: Bob Thompson 2019년 11월 20일
The basic usage of strcmp is to specify where you are looking and then what you are looking for. For your case it would be something similar to the following.
if strcmp(tempfile.data.gender,'non-binary')
gender = 0;
end
I would agree that strcmpi is better if there are people making individual inputs (it will make accounting for different variations of the entry easier), and I assume that the entry format is similar.
Try this:
if strcmpi(tempfile.data.gender,'non-binary')
gender = 0;
elseif strcmpi(tempfile.data.gender, 'female')
gender = 1;
elseif strcmpi(tempfile.data.gender, 'male')
gender = 2;
end
I cannot test this with your data, so I am labelilng it UNTESTED CODE. It should work, however I cannot guarantee that.
Jen
Jen 2019년 11월 21일
Ah yes, that works perfectly and much more concise than my method. Thank you!
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bob Thompson
Bob Thompson 2019년 11월 20일

0 개 추천

Just to clarify, you have 'gender' as two separate variables, once as a variable called 'gender' which will have a numeric assignment, and once as a field in a nested structure?
strcmp and == will ultimately have the same result, but you are going to run into the size issue on both of them. I work around this by having a second condition to be met for size.
if length(tempfile.data.gender) > 6 & tempfile.data.gender == 'non-binary'
gender = 0;
elseif length(tempfile.data.gender) > 4 & tempfile.data.gender == 'female'
gender = 1;
elseif length(tempfile.data.gender) == 4 & tempfile.data.gender == 'male'
gender = 2;
In a similar manner, if those are guaranteed to be the only possible responses, you can just use the size comparison to assign the value.

댓글 수: 1

Jen
Jen 2019년 11월 21일
(reposted as comment)
Thanks for posting! Yes you're right in that there are two variables called gender at play here - one nested in my data structure, the other in my output as a coded version of the first.
I ran your suggestion but still get the error 'Matrix dimensions must agree' though I can see why this workaround should work, as in theory matlab would only be comparing strings of 6 or more for the first loop etc.
In the mean time though I have come up with the following solution which gives me the output I was hoping for:
STR = tempfile.data.gender;
for p = "n"
if startsWith(STR,p) == 1
gender = 0;
end
end
for p = "f"
if startsWith(STR,p) == 1
gender = 1;
end
end
for p = "m"
if startsWith(STR,p) == 1
gender = 2;
end
end
I'm looking to make this work with the most concise code possible so any thoughts on this are very welcome! I know there's always many ways to achieve the same goal in Matlab, and my code may be a little clunky though it gets the job done.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

Jen
2019년 11월 20일

댓글:

2019년 11월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by