How would I turn this structure array into a matrix
Show older comments

I tried this, but i got an error
A = ([topTenWords.word],[topTenWords.frequency])
Answers (2)
Ameer Hamza
on 15 Apr 2020
Edited: Ameer Hamza
on 15 Apr 2020
If you want to have different data types then you need to use cell array
A = [{topTenWords.word}' {topTenWords.frequency}']
If you want to save as matrix, you need to use seperate variables
A_word = {topTenWords.word}';
A_freq = [topTenWords.frequency];
6 Comments
Brian Leon
on 15 Apr 2020
Ameer Hamza
on 15 Apr 2020
Brian, try this
A_word = categorical({topTenWords.word}');
A_freq = [topTenWords.frequency];
subplot(1,2,2)
bar(A_word, A_freq, 'FaceColor',[0.8500 0.3250 0.0980])
Brian Leon
on 15 Apr 2020
Ameer Hamza
on 15 Apr 2020
Brian, A_word in my answer already gives you a cell array. The issue here is that you were trying to set the wrong property. Try this code
A_word = categorical({topTenWords.word}');
A_freq = [topTenWords.frequency];
ax = subplot(1,2,2);
bar(A_word, A_freq, 'FaceColor',[0.8500 0.3250 0.0980])
title('Most Frequent Words')
ylabel('Word Frequenices')
ax.XAxis.Categories = A_word;
xtickangle(90)
Brian Leon
on 15 Apr 2020
Ameer Hamza
on 15 Apr 2020
I think that the vector [topTenWords.frequency] is not always sorted. try this.
A_word = categorical({topTenWords.word}');
A_freq = [topTenWords.frequency];
[A_freq, idx] = sort(A_freq, 'descend');
A_word = A_word(idx);
ax = subplot(1,2,2);
bar(A_word, A_freq, 'FaceColor',[0.8500 0.3250 0.0980])
title('Most Frequent Words')
ylabel('Word Frequenices')
ax.XAxis.Categories = A_word;
xtickangle(90)
Star Strider
on 15 Apr 2020
Edited: Star Strider
on 15 Apr 2020
You did it correctly in the files in How can i remove from a cell array, the words in another cell array?
EDIT — (15 Apr 2020 at 18:51)
Try this:
D3 = load('topTenWords.mat');
word = cellfun(@(x)x, {D3.topTenWords.word}, 'Uni',0)
frequency = cellfun(@(x)x, {D3.topTenWords.frequency});
figure
bar(frequency)
set(gca, 'XTickLabel',word, 'XTickLabelRotation',30)
producing:

Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
