Split table into Chunks.

49 visualizaciones (últimos 30 días)
Saria Ahmad
Saria Ahmad el 25 de Oct. de 2022
Comentada: Stephen23 el 30 de Oct. de 2022
Hi, I have a large table. I want to split the table into multiple chunks via loop and save its result in workspace so that later I can add into the powepoint slides in Matlab. But the subtables stores in .mat file. I want to save them into workspace as a table.
how can I do it. Any help would be great.
Thanks
I have written the following code:
T = T_measfine;
chunkSize = 8; % chunk size from number of rows
noOfChunks = ceil(size(T,1) / chunkSize)
% %% To Output chunks
for idx = 1:noOfChunks
if idx == noOfChunks
subtable = T(1:end,:)
else
subtable = T(1:chunkSize,:)
savefile = strcat('subdata',num2str(idx));
save(savefile, 'subtable')
end
end
  5 comentarios
Saria Ahmad
Saria Ahmad el 25 de Oct. de 2022
the reason behind this: sometimes the resultant tables becomes very large and when I get the automated result file that large table doesn't fit to the slide. So Idea is to split the tables and then add them to slides.
Walter Roberson
Walter Roberson el 25 de Oct. de 2022
yes but how does that require that different variables be used for each table? Instead of having a cell array of tables for example?

Iniciar sesión para comentar.

Respuesta aceptada

Askic V
Askic V el 25 de Oct. de 2022
Hello,
you're probably want something like this:
T = randi(10, 43,3);
chunkSize = 8; % chunk size from number of rows
noOfChunks = ceil(size(T,1) / chunkSize);
[rows, col] = size(T);
start_idx = 0;
for idx = 1:noOfChunks
if idx == noOfChunks
endpoint = rows;
else
endpoint = start_idx + chunkSize;
end
eval(sprintf('subTable%d = T(%d:endpoint,:)', idx, start_idx+1));
start_idx = chunkSize*idx;
end
subTable1 = 8×3
10 5 7 5 9 3 1 6 1 6 2 1 1 6 8 10 7 7 5 5 10 5 6 5
subTable2 = 8×3
2 2 2 5 9 2 5 10 6 3 6 1 4 2 8 6 9 10 3 6 6 3 2 10
subTable3 = 8×3
7 3 8 1 3 3 4 4 8 8 6 4 1 3 3 6 6 8 6 4 10 6 6 7
subTable4 = 8×3
7 7 9 8 3 6 10 8 10 6 10 5 7 1 6 10 5 2 9 1 9 9 9 10
subTable5 = 8×3
2 4 8 6 10 1 9 7 6 8 6 10 1 6 2 3 4 3 7 6 7 8 4 4
subTable6 = 3×3
5 9 6 6 5 3 10 8 7
If this is what you want, I need to warn you that this practice is highly unrecommended. YMATLAB arrays (for example cell) will let you do the same thing in a much faster, much more readable way.
  2 comentarios
Saria Ahmad
Saria Ahmad el 25 de Oct. de 2022
Thanks, Yes. This is what I want.
How can adapt in matlab arrays? Then I also have to adapt that subtables in power point slides via matlab. so then the problem arises.
Stephen23
Stephen23 el 30 de Oct. de 2022
"How can adapt in matlab arrays?"
By more slow, complex, inefficient, obfuscated code using the approach that you used to generate all of those variables. Bad data design forces you into writing bad code.
If you had sensibly used indexing as Askic V showed, then this would be simpler...

Iniciar sesión para comentar.

Más respuestas (1)

Askic V
Askic V el 25 de Oct. de 2022
Editada: Askic V el 25 de Oct. de 2022
I'm not really sure how you want to add subtables to power point slides, but you can create subtables in the following way:
T = randi(10, 53,3);
chunkSize = 8; % chunk size from number of rows
noOfChunks = ceil(size(T,1) / chunkSize);
rows = size(T,1);
cellArray = cell(0,3);
subTableNames = {};
for ii = 1:noOfChunks
start_idx = (ii-1)*chunkSize+1;
if ii == noOfChunks
cellArray{ii} = T(start_idx:end, :);
else
cellArray{ii} = T(start_idx:start_idx+chunkSize-1, :);
end
subTableNames{ii} = ['T', num2str(ii)];
end
T2 = cell2table(cellArray(:).', 'VariableNames', subTableNames(:))
T2 = 1×7 table
T1 T2 T3 T4 T5 T6 T7 ____________ ____________ ____________ ____________ ____________ ____________ ____________ {8×3 double} {8×3 double} {8×3 double} {8×3 double} {8×3 double} {8×3 double} {5×3 double}
T2.T1
ans = 1×1 cell array
{8×3 double}
For the purpose of power point slide, I guess the above suggested solution with evail is fine.
However, for other applications, please read this:
https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval

Categorías

Más información sobre Tables en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by