How can I split a cell that contains two different cells and store them with two different names?

1 view (last 30 days)
I am trying to find a way to split a cell that contains two different cells and save them with two different names within a for loop.
For example:
I have the following cell:
A={{55 55 44; 44 66 22} {44 22 00; 82 65 12}};
This one will produce the following
I want to extract the first cell (i.e, A{1,1} and store it in a matrix B_1 and then extract the second cell and store it in a matrix B_2 within a for loop.
I tried the following code:
A={{55 55 44; 44 66 22} {44 22 00; 82 65 12}};
for i=1:length(A)
B_{i}=A{1,i}
end
This code will give the same values of A.
In my case:
I want the output to be:
B_1= "2x3 cell" which means {55 55 44; 44 66 22}}
B_2="2x3 cell" which means {44 22 00; 82 65 12
Thanks in advance!
  4 Comments
Voss
Voss on 28 May 2022
"length of A is not constant and this is the reason why I want to store them in variable names."
To me the length of A not being constant seems like a reason not to store them in separate variables.
Regardless of the length of A, just use indexing into A, like I showed.
for ii = 1:numel(A)
% do something with A{ii}
end
Stephen23
Stephen23 on 28 May 2022
Edited: Stephen23 on 28 May 2022
"However, in my code, the length of A is not constant and this is the reason why I want to store them in variable names."
That is not a reason for storing meta-data (e.g. pseudo-indices) in variable names.
It is a good reason for using actual indices, as Jan and Image Analyst already explained in some detail.
Your approach will force you into writing slow, complex, inefficient code that is hard to debug. In contrast, using indexing is neat, simple, and very efficient. You should use indexing.

Sign in to comment.

Accepted Answer

Jan
Jan on 28 May 2022
Your example does not need a FOR loop, but the solution is trivial:
A = {{55 55 44; 44 66 22} {44 22 00; 82 65 12}};
B1 = A{1};
B2 = A{2};
But what is the benefit of using "B1" compared to "A{1}"?
I guess, that you want to do this in a loop, because A has much moire elements. Then the automatic creation of variables would cause serious troubles. It would work with eval, but this is an programming anti-pattern. It causes more problems than it solves. See: TUTORIAL: Why Variables Should Not Be Named Dynamically (eval)
A{1} is the perfect and efficient solution already.
  2 Comments
Mahmoud Khadijeh
Mahmoud Khadijeh on 28 May 2022
Edited: Image Analyst on 28 May 2022
Yes, exactly.
Here, I am trying to simplify my question, but my code is more complicated. In the example above, I can use
B1 = A{1};
B2 = A{2};
Because I know the length of A.
However, in my code, the length of A is not constant, and this is the reason why I want to store them in variable names.
I am not sure if you can help me with that.
Thanks in advance
Jan
Jan on 28 May 2022
The solution is easy: Don't do this. It is a shot in your knee to use dynamically created variables, because they are extremely hard to debug and slow down the processing by up to a factor of 100. As said already: A{1} is perfect already and B1 has no realy advantage.
This is one of the most frequently questions asked by beginners. It has been discussed exhaustively several hundreds of times in this forum. The result is the same in each case: Use arrays and indices instead of hiding the indices in the names of variables.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 28 May 2022
See the FAQ:
It will show you how to do it for more, and explain why you should NOT do it.
  8 Comments
Image Analyst
Image Analyst on 28 May 2022
Again, what do the 2 columns represent? x and y? If so, then why can't you just plot each cell independently to get that plot
[rows, columns] = size(K)
for col = 1 : columns
thisMatrix = K{1, col};
x = thisMatrix(:, 1);
y = thisMatrix(:, 2);
plot(x, y, '-', 'LineWidth', 2);
hold on;
end
legend;
grid on;
xlabel('x');
ylabel('y');
caption = sprintf('Plot of %d cells', columns);
title(caption);
hold off;
Mahmoud Khadijeh
Mahmoud Khadijeh on 28 May 2022
Edited: Mahmoud Khadijeh on 28 May 2022
Thank you so much for your help. This is what I need exactly. Yes, the two columns represent x and y.
I was trying to split them and then plot the matrices, and that cause some problems.
However, using your technique completly solved the problem.
Really appreciate your help!
Thanks

Sign in to comment.

Categories

Find more on Matrices and Arrays 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!