I've been trying to follow this answer in order to obtain unique strings from a given cell array. However, I'm running into trouble when iterating over these values. I have tried for loops as follows:
[unique_words, ~, occurrences] = unique(words);
unique_counts = hist(occurrences, 1:max(occurrences));
for a=1:numel(unique_words)
word = unique_words{a}
count = unique_counts{a}
result = result + a_struct.(unique_words{a}) + unique_counts{a}
end
When trying to reference the items like this, I receive the error:
Cell contents reference from a non-cell array object.
Changing the curly brackets to round brackets for unique_couts
yields the error:
Reference to non-existent field 'N1'.
Changing both unique_words
and unique_counts
to round brackets yields:
Argument to dynamic structure reference must evaluate to a valid field name.
How am I to iterate over the results of unique?
unique_words
is a cell array. unique_counts
is a vector. So unique_words
should be accessed using curly brackets and unique_counts
using round ones. The error that you are getting in this case is related to the a_struct
(which is not defined in the question) not having the corresponding field, not the access method.