Replacing a cell value in a struct
이전 댓글 표시
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!
채택된 답변
추가 답변 (2개)
Image Analyst
2021년 1월 19일
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
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
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.
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
