How to append a large number of cell arrays vertically?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Behrooz Daneshian
el 19 de Mzo. de 2023
Editada: the cyclist
el 19 de Mzo. de 2023
Hi all,
I have a large number of cell arrays that I need to append all of them vetically into a unique cell array. I do not want to load each of cell array individually and then append all of them. Can anyone tell me how I can do it in the most efficient way?
Thanks in advance for your asnwers.
2 comentarios
the cyclist
el 19 de Mzo. de 2023
We need more information. How are the individual cell arrays stored? (For example, are all the cell arrays already in the workspace, or do they need to be loaded from MAT files?) How are they named? For example, if your cell variables are named cellFred, cellWilma, cellBarney, and cellBetty, then there is no choice but to name them each individually to append them:
bigCell = [cellFred; cellWilma; cellBarney; cellBetty];
But maybe they have some naming convention that helps.
Are you just trying to avoid typing all the names? I mean, there isn't really a command for "append all files" that isn't somehow naming all the files to be appended.
Respuesta aceptada
Matt J
el 19 de Mzo. de 2023
Editada: Matt J
el 19 de Mzo. de 2023
s=dir('stations_*.mat');
CellList=cellfun(@(z) load(z).stations1,{s.name},'uni',0);
result=vertcat(CellList{:})
3 comentarios
Matt J
el 19 de Mzo. de 2023
z is the name I've chosen for the input to the function,
fun=@(z) load(z).stations1
the cyclist
el 19 de Mzo. de 2023
Editada: the cyclist
el 19 de Mzo. de 2023
Just to expand @Matt J's answer a bit. He used an anonymous function to accomplish the task. z is a dummy variable that will be substituted out by the values in the cell array (from the second argument to cellfun).
A simple example of an anonymous function is
func_sqr = @(x) x.^2
func_sqr([2 3 5])
You can see that x (like z in Matt's example) is just a placeholder variable in the function definition.
Más respuestas (0)
Ver también
Categorías
Más información sobre Function Creation 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!