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

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 Kommentare

Why do you want to do this?
Instead of having variables B_1 and B_2, you can refer to A directly, as in A{1,1} and A{1,2} (or just A{1} and A{2} in this case).
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
"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
"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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

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 Kommentare

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
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

See the FAQ:
It will show you how to do it for more, and explain why you should NOT do it.

8 Kommentare

Jan is absolutely right. However, there are only a few instances where I might pull out columns or rows for convenience in coding and that is where the number of things you pull out is very small, like 2 or 3 things. AND you know that there is a fixed small number, not like 20 or 100 or some unknown number of columns that you don't know until you check the array size. Like
xyTimeData = readmatrix(fileName); % Columns are x, y, and time point.
% Extract variables for convenience in later coding.
x = xyTimeData(:, 1); % Extract x data.
y = xyTimeData(:, 2); % Extract y data.
times = xyTimeData(:, 3); % Extract time point data.
I'm only doing that because I know in advance that there will be 3 columns, not some unknown number, and I know that I want to use simple variable names like "x" instead of "xyTimeData(:, 1)" because it's more convenient and intuitive.
Imagine if the array had anywhere up to 100 columns and somewhere later in the code you want to refer to col73 and use it somehow. Well, what if there was no column 73 because the matrix happened to be only 50 columns wide? The code would fail when you try to use col73.
Thank you all for your answers.
Mayble, I will try to explain that in a different way.
My code will generate the following cells:
My target is to convert these cells to a matrix X and then plot the data of this matrix
For example, in the above image after converting the cells to one matrix:
The first and second coumns will give the first curve
The third and fourth columns will give the second curve
The fifth and sixth columns will give the third curve.
However, I can only convert these cells to a matrix X if the dimensions are consistent
I can only use the function " X=cell2mat(K)" if all the cells have the dimensions.
In the above image, the first cell is 7467x2 double while the second and third cells are 7468x2 double.
So, I will not be able to convert these cells to one matrix and I will not be able to plot them.
This is why I was thinking about storing them in different names.
Is there any alternative way to solve this problem?
Thanks in advance!
We have explained it repeatedly and posted links to the page, where it is explained exhaustively:
Simply use K{1}, K{2}, K{3}. There is no need to store these data in new variables. Creating a variable called B1 has severe disadvantages, while K{1} can be called perfectly and efficiently e.g. in a loop also using the loop counter as index. There is no "alternative way" to solve this problem, but if you simply use K{1}, ... there is no problem at all.
"My target is to convert these cells to a matrix X and then plot the data of this matrix" - What is the advantage of drawing a matrix, if the vectors have different lengths? You can plot the different vectors in a loop instead using the loop counter as index.
Trust the very experiences programmers of this forum. The TUTORIAL: Why Variables Should Not Be Named Dynamically (eval) has rank 6 of the most voted answers.
@Mahmoud Khadijeh you mention columns 5 and 6. Well K is an array (matrix) with 1 row and 3 columns.
K{1,1} is a matrix with 7467 rows and 2 columns.
K{1,2} is a matrix with 7468 rows and 2 columns.
K{1,3} is a matrix with 7468 rows and 2 columns.
None of the arrays/matrices has 6 columns.
Exactly what would you like to plot against what?
In the matrices (inside each cell), exactly what do the two columns represent?
Also, explain WHY the first cell contains a matrix with one less row than the matrices in the other two cells.
Thanks @Image Analys.
K{1,1} is a matrix with 7467 rows and 2 columns --> the number of rows is 7467 and the number of columns is 2
K{1,2} is a matrix with 7468 rows and 2 columns.--> the number of rows is 7468 and the number of columns is 2
K{1,3} is a matrix with 7468 rows and 2 columns. --> the number of rows is 7468 and the number of columns is 2
if we converted the whole cell K (1x3) which contains {7467 rows and 2 columns , 7467 rows and 2 columns ,7467 rows and 2 columns} to matrix using the function cell2mat .. for example X=cell2mat(K)
This will generate a matrix with 6 columns.
However, I have to do something for the rows because the number of rows in K{1,1} ~= K{1,2} and K{1,3}
I am doing that because my objective is to generate a code that can plot different matrices in which the dimensions are not consistent.
Similar to the below image in Excel:
So, K{1,1} in my case should generate the matrix of the first two columns in this image A & B
K{1,2} --> D & E
K{1,2} --> G & H
Having them all together in one matrix in MATLAB is imposible.
I know that we can use the function plot(K(:,1),K(:,2),K(:,3),K(:,4),K(:,5),K(:,6)) but I have to split them first.
Thanks,
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;
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by