# How to make the sizes of all variable same in the loop.

1 view (last 30 days)
MakM on 4 Jan 2022
Commented: MakM on 4 Jan 2022
I have 4 variables, Game0, Game1, Game2, Game3 in the for loop. I am writing these variables to excel file by using the "writetable" function. All the variables size should be same if I am writing to the excel file. How can I do that. I am using Game1(numel(Game0))=0, but this is not correct because sometimes Game0 has no value.
KSSV on 4 Jan 2022
See whose length is maximum ou t of four and append zeros to other variables.

KSSV on 4 Jan 2022
% Demo data
Game0 = rand(5,1) ;
Game1 = rand(7,1) ;
Game2 = rand(9,1) ;
Game3 = rand(3,1) ;
a = {Game0 Game1 Game2 Game3}; %test data
len = max(cellfun('length',a));
b = cellfun(@(x)[x;zeros(len-size(x,1),1)],a,'UniformOutput',false);
Game = [b{:}] ;
T = array2table(Game)
T = 9×4 table
Game1 Game2 Game3 Game4 _______ _______ _______ _______ 0.50908 0.3977 0.91829 0.52539 0.46445 0.28806 0.7916 0.56689 0.5089 0.95957 0.98846 0.34577 0.77494 0.40111 0.6214 0 0.38022 0.19804 0.37658 0 0 0.63789 0.1338 0 0 0.42852 0.80441 0 0 0 0.5414 0 0 0 0.49234 0
Note that there will be change in the variable name. Game1 in table is Game0.
MakM on 4 Jan 2022
Got it thanks.