Searching for specific word containing specific letters and removing last element in the string
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have a wordbank containing only one word for every line. I want to sort of the words in struct, such that wordbank.a=airplane, where the beginning of the word that is stored in the struct starts with its field name, 'a' and then i'll have wordbank.b=ball....[row,1] array and for 'c' 'd' until the letter 'z'.. my question is, how do I make sure that the word that are stored with a specific goes to the right field of letters?
another question is,
if i have a string a=catss,and I want to make it a=cats only.How do I find the words with 's' at the end of the each word so that I could remove every word all plural forms of a word that end in s. If any word that is checked contain word+s, thre plural form must be removed.
0 Commenti
Risposta accettata
Daniel Shub
il 8 Nov 2011
For the first part of your question:
wordlist = {'airplane', 'ball', 'bell', 'bull'};
letters = 'a':'z'; % a, b, c, ..., z
wordstruct = struct;
for ichar = 1:length(letters)
wordstruct.(letters(ichar)) = cell.empty;
end
for iword = 1:length(wordlist)
wordstruct.(wordlist{iword}(1)){end+1} = wordlist{iword};
end
I have no idea what you are asking in the second part. Maybe something like:
x = wordlist;
for iword = 1:length(wordlist)
if strcmp(wordlist{iword}(end), 's')
x{iword} = wordlist{iword}(1:(end-1));
end
end
11 Commenti
Più risposte (1)
Walter Roberson
il 20 Nov 2011
Examine your code,
for ichar = 1:length(letters)
wordbank.(letters(ichar))=cell.empty;
wordbank.(tline(1)) = tline;
end
Now suppose tline(1) is 'b'. Then on the first loop over ichar, wordbank.(letters(1)) = cell.empty; so you would set wordbank.a to cell.empty; Then on the line below that, wordbank.(tline(1)=tline; so wordbank.b=tline. Now, the second iteration of the ichar loop, wordbank.(letters(1)=cell.empty; so you would set wordbank.b=cell.empty; but that would be overwriting the wordbank.b=tline of the loop above. But then again because of the wordbank.(tline(1))=tline; you would set wordbank.b=tline; Anyhow, keep going this way and you end up with with wordbank.a and .b and .c and so on to .z all set to cell.empty except for the one character that tline starts with, which will have been set to tline (which is a string, not a cell.) And it looks like you are in a loop so it looks like you will repeat this process of clearing back to {} except for the last tline processed.
By the way, when you trim the "plural" form of words, what is going to happen to the word "loss" or to "was" or "yes" or "is" or "us" ?
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!