Need a quick help on storing words according to lengths with struct

Given a wordlist in cell array
wordlist={'blues';'rhyme';'armor';'dinosour';'book';'mail'......};
wordstruct=struct;
%%i am not sure how to proceed however it is organised such that the words are between four to 25, and I would like to do something like this,
wordstruct.4L={book mail ....}
wordstruct.5L={blues rhyme.....}
.
.
.
.
.
wordstruct.26L={whateveritisthatmightexist}
I almost given up on finding the right way to do it this way. Any help is very appreciated!

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 11월 27일
You have to use wordstruct.L4, wordstruct.L5, etc. as '4L','5L' etc. are not valid name.
cellfun('islength',...) and then a for-loop should getting you there.
wordlist={'blues';'rhyme';'armor';'dinosour';'book';'mail'};
clear wordstruct;
Len=cellfun('length',wordlist);
for L=4:26
wordstruct.(['L',num2str(L)])=wordlist(Len==L);
end

댓글 수: 4

I do realize that it is not a valid name, but I am not sure how should I change it. For your information, there will be at most 250k of words and i have to sort the words according to their word length and later, find out how many words have a certain word length.
I am not sure why but the function cell2str is not defined in the matlab program. Perhaps its been updated to a different function name?
Sorry, I meant cell2struct(). In fact, it doesn't have to be used. See update.
you are awesome!Thank you!

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by