How can I split a cell that contains two different cells and store them with two different names?
Ältere Kommentare anzeigen
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
Voss
am 28 Mai 2022
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).
Mahmoud Khadijeh
am 28 Mai 2022
Voss
am 28 Mai 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
"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.
Akzeptierte Antwort
Weitere Antworten (1)
Image Analyst
am 28 Mai 2022
2 Stimmen
See the FAQ:
It will show you how to do it for more, and explain why you should NOT do it.
8 Kommentare
Image Analyst
am 28 Mai 2022
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.
Mahmoud Khadijeh
am 28 Mai 2022
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
am 28 Mai 2022
Image Analyst
am 28 Mai 2022
@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.
Mahmoud Khadijeh
am 28 Mai 2022
Bearbeitet: Mahmoud Khadijeh
am 28 Mai 2022
Image Analyst
am 28 Mai 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
am 28 Mai 2022
Bearbeitet: Mahmoud Khadijeh
am 28 Mai 2022
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

