How do I use a loop to go through a large dataset and put each column into a array?

14 vues (au cours des 30 derniers jours)
I'm doing a project that needs to process large datasets. My data is taken from a table with headings and I need each column to be made into a separate array to then plot graphs from.
Say my dataset is 1300 x 85 table
It's too time consuming to write out the code for each column separately using my current method:
table=readtable('myfile.csv"); % import file as a table
C1=table{:,1}: % make a singular column into array
histogram(C1) % plot graph using array
Instead, can I use a loop to take table of length, x and step through each column itself and define them as separate variables?
Would an if or for loop be more suitable?
  1 commentaire
Star Strider
Star Strider le 30 Jan 2023
Instead, can I use a loop to take table of length, x and step through each column itself and define them as separate variables?
Please do not ever do that!
Would an if or for loop be more suitable?
Yes with for!
The if, elseif, else is not a loop. It is a logical decision block.
.

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 30 Jan 2023
You don't need (and shouldn't even think about creating) another array from the table; just use the data as they are in the table.
tTable=readtable('myfile.csv'); % read the table; name as table for code documentation
for i=1:size(tTable,2) % loop over how many columns there are...don't hard code
vN=string(tTable.Properties.VariableNames(i)); % get each variable name in turn as string
hF(i)=figure; % create a new figure for each in turn
hH(i)=histogram(tTable.(vN)); % and histogram each
title(vN) % and put on a title
end
You can finish dressing up the plots as desired with x/y labels and whatever other annotations are needed.
Obviously, you can let a user (or you) first select a list of variables you would want to do rather than all 85 in one go...the possibilities are endless.
See the link under the Tables data type on addressing table variables to see the "veritable plethora" of addressing modes and look through the list of functions associated with the table class at the top of the doc page to see the many tools available for working with and analyzing data in tables.
Whatever you do, don't start creating a sequence of named variables as you outlined above...
  2 commentaires
Lucy Perry
Lucy Perry le 31 Jan 2023
Thank you!
I made an array from the table orignally because I didn't know you could use the dot notation for plotting from a table
But the example you've given me works and plots quickly which is exactly what I was looking for :)
dpb
dpb le 31 Jan 2023
"...I didn't know you could use the dot notation for plotting from a table "
What you're doing with a table variable has no bearing on the addressing/returning the variable from the table and using it just as any other variable...it seems to be a very common belief that once the data are in a table it's mandatory to make copies of them to be used elsewhere instead of just using the data reference from the table.
As noted above, time invested reading the doc to gain an appreciation of how to use the various tools provided will pay off many times over in development time spent debugging and trying to put something together without using the power built into MATLAB.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by