How would I turn this structure array into a matrix

Respuestas (2)

Ameer Hamza
Ameer Hamza el 15 de Abr. de 2020
Editada: Ameer Hamza el 15 de Abr. de 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 comentarios

Okay this worked,thank you. My overall goal is to use this matrix to plot on a bar plot, but I get an overall error.
A = [{topTenWords.word}' {topTenWords.frequency}'];
subplot(1,2,2)
bar(A,'FaceColor',[0.8500 0.3250 0.0980])
Error using bar (line 127)
Input arguments must be numeric, datetime, duration or categorical.
Error in storyAnalyzer (line 60)
bar(A,'FaceColor',[0.8500 0.3250 0.0980])
My bar plot is suppose to look like this.
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])
Thank you. One last quesiton if you have time. I am trying to make this barGraph as I previously showed above but I get an error when running this code.
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])
title('Most Frequent Words')
ylabel('Word Frequenices')
set(gca,'XTick',[1:10],'xticklabel',topTenWords)
xtickangle(90)
The error is because my topTenWords is a 1x10 structure array. Is there any way I can turn this structure array into a cell array with just the contents on the left(topTenWords.word)
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
Brian Leon el 15 de Abr. de 2020
This code doesnt order the bar plot from highest frequency to lowest frequency, from left to right. This was my output.
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)

Iniciar sesión para comentar.

Star Strider
Star Strider el 15 de Abr. de 2020
Editada: Star Strider el 15 de Abr. de 2020
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:

Categorías

Etiquetas

Preguntada:

el 15 de Abr. de 2020

Comentada:

el 15 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by