Accessing the contents of a 1x1 matlab cell

社会主义新天地 提交于 2019-12-02 01:46:13

问题


I'm not sure about the terminology, but I have read data from a text file into a 1x1 cell array P. When examining P, it lists "<142x2 cell>" in the (1,1) position. From there I can double-click this and it opens up into the 142x2 cell that I actually want. The issue is, I don't get how to manipulate this data via code to convert from the 1x1 cell array to the 142x2 cell array. Also, I cannot find anywhere what the curly brackets denote.


回答1:


I don't get how to manipulate this data via code to convert from the 1x1 cell array to the 142x2 cell array.

The cell array P is actually a 1x1 cell array, which in turn contains another cell array 142x2. This type of output is very common when using textscan. To access the inner cell array, you can simply use curly braces ({}), like so:

Q = P{1}; // or P{:} if you're certain that P holds only one cell

The resulting Q should hold your 142x2 cell array. I usually "flatten" P by doing P = P{:}, without using an intermediate variable.

Also, I cannot find anywhere what the curly brackets denote.

Have you read MATLAB's documentation about special characters? Here's what it says:

Curly braces are used in cell array assignment statements. For example, A(2,1) = {[1 2 3; 4 5 6]}, or A{2,2} = ('str'). See help paren for more information about { }.

I would also urge you to read the following (very) related question: Difference between accessing cell elements using {} and () (curly braces vs. parentheses)




回答2:


Short answer: You can assign the content of the first cell in P to P.

Example:

P = {cell(142,2)}; %Create a 142x2 cell inside a cell
P = P{1};          %Solution: Now P is a 142x2 cell

If you try help cell it will lead you to help paren that explains the use of curly brackets.



来源:https://stackoverflow.com/questions/19568433/accessing-the-contents-of-a-1x1-matlab-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!