Use Splitapply to write multiple files

2 visualizaciones (últimos 30 días)
Sanjay
Sanjay el 19 de Nov. de 2019
Comentada: Sanjay el 19 de Nov. de 2019
I have grouped tables by a variable and I am trying to write multiple files based on the grouping variable. But it does not work.
I used findgroups and splitapply. But the splitapply is where I am having problems.
Here is one version of the commands I am using
load patients;
g=findgroups(Gender)
func=@(x,y) csvwrite(x,y)
splitapply(func,Gender,Weight,G)
When I figure out how to use this, I will be using it on large datastore tall arrays. Please help !

Respuesta aceptada

Stephan
Stephan el 19 de Nov. de 2019
Editada: Stephan el 19 de Nov. de 2019
Here is a small example of how it could work:
% some data to play with
Gender = ['m' 'm' 'w' 'w' 'm' 'w']';
Age = randi(60,6,1) + 15;
Weight = randi(100,6,1) + 20;
Height = randi(round(80/25.4),6,1) + round(110/25.4,1);
% create a table of the data
T = table(Gender,Age,Weight,Height)
% find groups depending on the gender
T.groups = findgroups(T.Gender);
% Write the 2 groups in 2 excel files depending on the gender group
% The file names here are "Table_1.xls" and "Table_2.xls" and both are
% stored in the current working folder
for k = 1:max(T.groups)
writetable(T(T.groups==k,1:end-1),['Table_' num2str(k)],'FileType','spreadsheet')
end
  1 comentario
Sanjay
Sanjay el 19 de Nov. de 2019
Hello Stephan, thank you for taking the time! Your code will work for me.
My hope still is that I could write the files without using a loop with splitapply..

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Workspace Variables and MAT-Files 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