How can I split a cell that contains two different cells and store them with two different names?
1 次查看(过去 30 天)
显示 更早的评论
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1014190/image.jpeg)
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 个评论
Voss
2022-5-28
"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
2022-5-28
编辑:Stephen23
2022-5-28
"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.
采纳的回答
Jan
2022-5-28
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 个评论
Jan
2022-5-28
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.
更多回答(1 个)
Image Analyst
2022-5-28
See the FAQ:
It will show you how to do it for more, and explain why you should NOT do it.
8 个评论
Image Analyst
2022-5-28
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;
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!