How to pull out data from cell array, concatenate it, and put into table for varying trials
Show older comments
Hello there, I have data from 10 trials stored in a 1x10 cell array "Predictors" and I want to create a loop that pulls out one trial at a time and then concatonates the data in all the other trials and saves it under a unique or numbered variable name? I am thinking something like this. I am not sure how to have it number each iteration:

for i = 1:length(Predictors)
Test_i = Predictors(i)
trainindicies_i = setdiff(1:length(Predictors),i);
Train_i = Predictors(trainindicies)
TrainX_i = vertcat(Train_i{:})
end
8 Comments
Stephen23
on 28 Jul 2024
"... and saves it under a unique or numbered variable name?"
Only if you want to force yourself into writing slow, complex, inefficient, obfuscated, buggy code that is hard to debug:
The neat and efficient approach is to use indexing. You should use indexing.
Hi @Isabelle,
You have to modify the provided loop in your code as follows to concatenate data from multiple trials in Matlab, here is the updated example code.
% Given 1x10 cell array "Predictors" with data from 10 trials
Predictors = cell(1, 10); % Sample data initialization
for i = 1:length(Predictors)
% Extract data for the i-th trial
Test_i = Predictors{i}; % Access data for the current trial
% Concatenate data from all other trials
trainIndices_i = setdiff(1:length(Predictors), i);
Train_i = Predictors(trainIndices_i);
TrainX_i = vertcat(Train_i{:});
% Save concatenated data under a unique or numbered variable name
uniqueVarName = ['ConcatenatedData_' num2str(i)]; % Generate unique variable name
assignin('base', uniqueVarName, TrainX_i); % Save concatenated data in the workspace
% Display results for each iteration
disp(['Iteration ' num2str(i) ' - Concatenated Data:']);
disp(TrainX_i); % Display concatenated data
% Additional processing or analysis can be added here
end
Hope this answers your question.
@Stephen23 has the right idea; while you CAN create all these named variables, once you have done so, then you have no good way to process them other than by continuing down the path in which "there be dragons!".
Instead, use arrays as they're intended in MATLAB (it is, after all, "MATrix LABoratory" for a very good reason)...
However, to be sure about the solution, we need to know what the content of the arrays in each cell is; it is a 2D array, what is in each dimension?
Also NOTA BENE -- the use of length on the content of the ith cell will return the number of columns in the array, NOT the number of rows because the definition of length(x) is the max(size(x)) which for a 63x541 array will return 541. Is that the intent? -- I'm guessing not. Use size() with the optional specific dimension of interest; length is a quite dangerous function but exists as is owing to historical precedent and the need to keep backwards compatibility trumps modern design precepts.
Umar
on 28 Jul 2024
@dpb,
So, to determine the length along a particular dimension, it is recommended to use the size() function with the dimension of interest specified such as to get the number of columns in a 63x541 array A, you can use size(A, 2).
" to determine the length along a particular dimension, it is recommended to use the size() function with the dimension of interest specified..."
Not only is it recommended but it is mandatory to use the size() function to ensure getting the dimension of interest. As documented, length returns the max(size(x)) which could be any dimension size depending upon the actual array dimensions.
x=rand(3,2);[length(x) size(x) numel(x)]
x=rand(2,3);[length(x) size(x) numel(x)]
x=rand(2,1,3);[length(x) size(x) numel(x)]
The above is what size(_,1), size(-,2),size(_,3) will return all for the same function call; all just dependent upon the size and orientation of the input array.
length works reliably only for vectors; there are the two corollary functions height and width but they are only for tables. I've suggested they be overloaded for 2D arrays as well, but so far TMW has not seen fit to do so; probably because arrays can be N-dimensional so it isn't possible to completely generalize. My proposal was to go the 3D as the most common set folks use with depth() for the 3rd dimension.
If you write/test code on an input array that is, say, 20x15, processing by row using
for i=1:length(X)
res=somefunction(X(i,:));
...
end
and then when you run it on real data the next file turns out to have only 12 rows, say, then BOOM! you'll crash and burn when try to process the 13th and non-existent row.
This is the good case; it does error and you can find (and fix) the logic error; the real danger is when the logic is such that doesn't error but silently just goes on but isn't doing what is expected...treating X as Y and vice versa, probably.
Walter Roberson
on 28 Jul 2024
there are the two corollary functions height and width but they are only for tables.
That is certainly the impression you would get by looking at the summary of the functions, which says (for example)
Number of table rows
However, as of R2020b, you can also pass arrays instead of tables. You can see this in the detailed description of the input arguments.
Huh. I asked for that enhancement ages ago but owing to that summary had never actually tried it and missed seeing it in a new features blurb (which, I admit, I generally don't bother to read which puts the blame directly onto me, granted!)
Thanks for pointing it out, I'll have to begin using them...I've always hated to have to use the clumsy (size,1).
I see they still haven't gone along with the idea for The Third Dimension, though... :(
Umar
on 29 Jul 2024
@dpb & @Walter, @Stephen23, sounds like Voss joined the party as well, and shared the code snippet that you guys were talking about. I want to thank everyone of you sharing your thoughts and glad to know that we are working as a team to help out each other.
Answers (1)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!