Plotting categorical array of varying size

10 visualizzazioni (ultimi 30 giorni)
Jarrod Arruda
Jarrod Arruda il 11 Gen 2022
Risposto: Shlok il 24 Ott 2024 alle 6:45
Hi all,
I have 3 array types I'm using to create a bar graph:
  • (variable c) a categorical array with character vectors where a type of category might exist more than once [created using built-in function categorical]
  • (variable x) a cell array containing the different types of vectors that exist in variable c [created using built-in function categories]
  • (variable xx) a double array, containing the number of occurrances variable c exists for each type of variable x [created using countcats]
I'm using bar(xx), and pareto(xx) to plot this data. However, I would like to plot this data from a folder of 1000+ txt files into a single figure, where each file doesn't necessarily contain common variable x. Therefore as I loop through each txt file, the categorical array rows columns.
Should I make this a cell array to manipulate here, or maybe use addcats function? I've written a function that works correctly for a single txt file, but I am unsure how to integrate these 3 array types to where I can plot aggregate data from all txt files.
Please let me know additional code you think may be helpful -- didn't seem useful to begin with.
Thanks,
Jarrod

Risposte (1)

Shlok
Shlok il 24 Ott 2024 alle 6:45
Hi Jarrod,
As per my understanding, you have multiple files containing categorical arrays, and you want to read through them and plot a histogram.
To do this, I suggest reading all the files, concatenating the categorical values into a global array, and then generating a histogram by extracting unique categories and their counts using the “categories” and “countcats” functions.
Here’s how you can achieve that:
1. Start by initializing a categorical array that will store data from all the files.
allData = categorical([]);
2. Now, loop through each file, read the data and convert the relevant column or portion of the data to a categorical array.
% 'data' is the output of readtable, which reads the file within the loop
curFileData = categorical(data.YourColumnName);
3. After reading the data from each file, concatenate the current categorical array with the main array.
allData = [allData; curFileData];
4. Use the “categories” function to find the unique categories in the concatenated array.
uniqueCategories = categories(allData);
5. Use the “countcats” function to get the count of each category.
counts = countcats(allData);
6. Finally, plot a bar graph using the counts for each category.
bar(counts);
xticklabels(uniqueCategories);
Refer to the following MathWorks Documentation link, to know more about countcats” function:
Hope this helps.

Categorie

Scopri di più su Categorical Arrays in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by