Hello,
I have a struct with 4 fields.
One field contains my conditions (3 conditions, named using strings)
Now I want to replace the strings with numbers. How is that possible? So far I could not find anything :( Is this even possible?
Help is much appreciated!

 채택된 답변

Image Analyst
Image Analyst 2021년 1월 19일

0 개 추천

This works:
% Identify what number should replace what character vector.
replacements = {'condi1', 1; 'condi2', 2; 'condi3', 3}
% Make cell array for Conditions
caConditions = {'condi1' 'condi2' 'condi3' 'condi3' 'condi1' 'condi3' 'condi2'}
% Create the whole structure array.
s = struct('subjectnumber', {1 1 1 1 1 1 1}, ...
'condition', caConditions, ...
'rt',{0.12 0.11 0.8 0.9 0.13 0.6 0.12}, ...
'point',{0 40 40 40 20 20 0})
for k = 1 : numel(s)
thisCondition = {s(k).condition}; % Character vector like 'condi1', or 'condi2', etc.
% Compare this condition to the list of them in column 1 of replacements
% and find out which row contains the replacement.
[~, row] = ismember(thisCondition, replacements(:, 1));
% Replace it with the number in column 2
s(k).condition = replacements{row, 2};
end

댓글 수: 2

deejt
deejt 2021년 1월 19일
편집: deejt 2021년 1월 19일
Thank you so much! This indeed solved my problem!!!
How would I proceed, if I had so many more entries (because 7 is very easy).
But at this part for example: what if I had 24778 entries - I can't possibly type in everything manually can I?
s = struct('subjectnumber', {1 1 1 1 1 1 1}, ...
'condition', caConditions, ...
'rt',{0.12 0.11 0.8 0.9 0.13 0.6 0.12}, ...
'point',{0 40 40 40 20 20 0})
Image Analyst
Image Analyst 2021년 1월 20일
편집: Image Analyst 2021년 1월 20일
Are you saying that you could have 25 thousand different strings? If so, rather than a look up table like I used, you'd need to use something like sscanf() to parse/extract the number out of the character string.

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

추가 답변 (2개)

Image Analyst
Image Analyst 2021년 1월 19일

0 개 추천

Yes. For example to set the "condition" field to the number 5, do this:
yourStruct.condition = 5;
It doesn't matter that it used to be a string.
Attach your structure in a .mat file, or give code to create it, if you still have problems.

댓글 수: 4

deejt
deejt 2021년 1월 19일
편집: deejt 2021년 1월 19일
thanks for the response, however, unfortunately, that's not quit it.
mydata= struct with fields:
time: [0 0 0 0 0 0 0 0 0 0]
name: 'a'
point: 3.1416
condition: 'fourth' 'fifth' 'sixth'
I do not want to change the name of the field but its corresponding values, i.e., 'fifth'to the numerical value 2.
Kind of have the feeling, that this might not be possible..
Bob Thompson
Bob Thompson 2021년 1월 19일
편집: Bob Thompson 2021년 1월 19일
Image Analyst's suggestion will change the value of the field, rather than the name. mydata.condition will still be called mydata.condition, it will just simply be a numerical value, instead of a string.
Are you looking to mix class values for the different values within the condition field? i.e. change 'fifth' to 2, but keep 'fourth' and 'sixth'? That's a bit more complicated, as you will need mydata.condition to contain cell class values, but it's certainly possible.
Are you looking to do a search and replace? i.e. check all mydata.condition values for 'fifth' and replace with 2?
Or are you trying to say that there is more data within 'condition' that you want to keep 'fifth,' but the related data should be changed to 2?
Image Analyst
Image Analyst 2021년 1월 19일
AGAIN:
Attach your structure in a .mat file, or give code to create it, if you still have problems.
I need it because I'm not sure how condition is what you showed : 3 character arrays. It can be a character matrix, a string vector, or even a cell array, but I don't know what you have or how you displayed that. Plus answer Bob's questions.
deejt
deejt 2021년 1월 19일
편집: deejt 2021년 1월 19일
@Image Analyst when I tried your suggestion I received an error msg unfortunately. I attached it here, maybe I am overseeing something! (I am new to programming and just trying to figure out some basics).
"Scalar structure required for this
assignment.
Error in TutorialSession (line 4)
s.condition = 5"

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

deejt
deejt 2021년 1월 19일
편집: deejt 2021년 1월 19일

0 개 추천

Thank you guys for the effort.
@Bob Thompson Yes, I am trying to do a search/find and replace. In every cell of the field block I want to change the name that is in string to a numerical value.
condi1 should become 1, condi2 should become 2, condi3 should become 3 in every cell these string appear.
Thank you!

카테고리

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

질문:

2021년 1월 19일

편집:

2021년 1월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by