MATLAB Answers

How to use create table name variables in loop?

6 views (last 30 days)
%% Divide into groups
group_total= round(n/K);
Group_name= {'Group1' 'Group2' 'Group3' 'Group4' 'Group5' 'Group6' 'Group7' 'Group8' 'Group9' 'Group10'};
for x=1:1:K
for g=1:1:group_total
num= x* g;
s.(Group_name{x}(g,:)) = randomdata(num,:);
I have a table that is 8143 x 10 (randomdata). I need to divide it into 10 smaller tables by row number. I should have 10 (approx) 814x10 tables. I'm trying to use s as struct but I don't know how to make the Group_name{x} a 814 x10 table instead of a 1x10 table.
K is the number I need to divide the main table(random data) by.
group_total is the number of rows divided by K
s is the struct

Accepted Answer

Star Strider
Star Strider on 12 Sep 2021
I am not certain that I understand what you want to do.
Perhaps —
randomdata = randn(33,10);
NrCellArrays = fix(size(randomdata,1)/10)-1;
for k = 1:NrCellArrays
idxrng = (1:10)+10*(k-1);
Group{k,:} = randomdata(idxrng,:);
Group{k+1,:} = randomdata(max(idxrng):size(randomdata,1),:)
Group = 3×1 cell array
{10×10 double} {10×10 double} {14×10 double}
In any event, just put them into a cell array and refer to them by subsecripts (such as Group{1} and so forth). If at all possible, so not use numbered variables if a collection of objects (such as here) is the desired result.
Star Strider
Star Strider on 12 Sep 2021
As always, my pleasure!
The example you wrote returns row 3 of Group 3.
To access Group 3, column 4:
See Access Data In A Cell Array for an extended description.

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!