3

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.

Eitan T
  • 32,660
  • 14
  • 72
  • 109
user2208604
  • 301
  • 2
  • 6
  • 15
  • What do you want? Its a 142x2 cell, what should be done with the second column? Cot off? Concatenate both columns to one? – Daniel Oct 24 '13 at 14:17
  • Sorry if I'm not clear Daniel. Right now the data type is a 1x1 cell, {P}. When I go to look at the data of {P}, the (1,1) location simply says "<142x1 cell>", if I double click on this, it opens up to the 142x1 data I actually want. I think my terminology may be a bit off, but does this make more sense? – user2208604 Oct 24 '13 at 14:21
  • 2
    related question: [Difference between accessing cell elements using {} and () (curly braces vs. parentheses)](http://stackoverflow.com/q/9055015/1336150) – Eitan T Oct 24 '13 at 14:29
  • Beginner's note. I had this issue when I had created a cell array by putting together headers and data. I had put curly brackets around it which was not necessary, deleting them let me avoid the {1} of the answers. – questionto42 Jan 03 '21 at 20:20

2 Answers2

7

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)

Community
  • 1
  • 1
Eitan T
  • 32,660
  • 14
  • 72
  • 109
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.

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122