Split table into Chunks.
22 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2022-10-25
yes but how does that require that different variables be used for each table? Instead of having a cell array of tables for example?
采纳的回答
Askic V
2022-10-25
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
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 个评论
Stephen23
2022-10-30
"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...
更多回答(1 个)
Askic V
2022-10-25
编辑:Askic V
2022-10-25
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.T1
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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!