How to use create table name variables in loop?

6 views (last 30 days)
%% Divide into groups
K=10;
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,:);
end
end
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,:);
end
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.
.
  5 Comments
Summer Bolton
Summer Bolton on 12 Sep 2021
That is exactly what I want, thank you so much!!
It also took me awhile to realize that to access the data inside, say Group 3 column 4, the syntax is Group{3}(3,:); for anyone else who has this question!
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:
Group{3}(:,4)
See Access Data In A Cell Array for an extended description.
.

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!