How to append a large number of cell arrays vertically?

4 Ansichten (letzte 30 Tage)
Behrooz Daneshian
Behrooz Daneshian am 19 Mär. 2023
Bearbeitet: the cyclist am 19 Mär. 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 Kommentare
the cyclist
the cyclist am 19 Mär. 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.
Behrooz Daneshian
Behrooz Daneshian am 19 Mär. 2023
All the cells are saved as Mat files existing in the directory folder. They are named as "stations_Abbreviation of each US state (e.g. stations_AZ, stations_CA, stations_CO, stations_CT, stations_DC). I have as many cell as the number states in the US. 5 cells are attached here as representative of other cells.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 19 Mär. 2023
Bearbeitet: Matt J am 19 Mär. 2023
s=dir('stations_*.mat');
CellList=cellfun(@(z) load(z).stations1,{s.name},'uni',0);
result=vertcat(CellList{:})
result = 1588×7 cell array
{'GHCND:MXN00002037'} {'1961-03-01'} {'2012-12-31'} {[0.7550]} {[ 39.9000]} {[32.7000]} {[-114.7167]} {'GHCND:USC00020060'} {'1924-05-30'} {'2019-02-28'} {[0.7052]} {[ 659.9000]} {[33.9433]} {[-113.1875]} {'GHCND:USC00020080'} {'1914-05-01'} {'2023-01-05'} {[0.8189]} {[ 533.7000]} {[32.3698]} {[-112.8599]} {'GHCND:USC00020100'} {'1975-07-01'} {'2022-12-31'} {[0.8258]} {[ 393.2000]} {[34.2285]} {[-113.5778]} {'GHCND:USC00020159'} {'1904-10-01'} {'2023-01-06'} {[0.7291]} {[2.4536e+03]} {[33.8492]} {[-109.1469]} {'GHCND:USC00020170'} {'1985-07-01'} {'2012-12-03'} {[0.1001]} {[ 2792]} {[33.6391]} {[-109.3277]} {'GHCND:USC00020287'} {'1942-12-01'} {'2023-01-05'} {[0.8068]} {[ 840.9000]} {[31.9793]} {[-111.3837]} {'GHCND:USC00020288'} {'1987-05-01'} {'2023-01-06'} {[0.8351]} {[ 630.9000]} {[33.4625]} {[-111.4813]} {'GHCND:USC00020309'} {'1923-07-01'} {'1990-04-30'} {[0.8353]} {[1.1247e+03]} {[31.8805]} {[-110.2403]} {'GHCND:USC00020404'} {'2002-03-01'} {'2018-08-31'} {[0.7877]} {[ 464.8000]} {[32.7306]} {[-111.6917]} {'GHCND:USC00020586'} {'1925-05-01'} {'2023-01-06'} {[0.7586]} {[1.1994e+03]} {[34.5975]} {[-113.1745]} {'GHCND:USC00020632'} {'1939-09-01'} {'2023-01-05'} {[0.7776]} {[ 502.9000]} {[33.8097]} {[-111.6497]} {'GHCND:USC00020670'} {'1915-11-01'} {'2007-06-30'} {[0.4496]} {[1.0744e+03]} {[34.6416]} {[-111.7830]} {'GHCND:USC00020671'} {'1996-02-28'} {'1996-11-30'} {[ 1]} {[ 571.5000]} {[36.8969]} {[-113.9425]} {'GHCND:USC00020672'} {'1956-08-01'} {'2020-04-16'} {[0.6760]} {[ 588.6000]} {[36.9139]} {[-113.9423]} {'GHCND:USC00020678'} {'1999-11-01'} {'2023-01-06'} {[0.8423]} {[2.1799e+03]} {[35.2302]} {[-111.8221]}
  3 Kommentare
Matt J
Matt J am 19 Mär. 2023
z is the name I've chosen for the input to the function,
fun=@(z) load(z).stations1
the cyclist
the cyclist am 19 Mär. 2023
Bearbeitet: the cyclist am 19 Mär. 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 = function_handle with value:
@(x)x.^2
func_sqr([2 3 5])
ans = 1×3
4 9 25
You can see that x (like z in Matt's example) is just a placeholder variable in the function definition.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by