10

Starting with the MATLAB char array, A:

A(1,1) = 'A'
A(1,2) = 'P'
A(1,3) = 'R'
A(2,1) = 'M'
A(2,2) = 'A'
A(2,3) = 'Y'

How can this be converted to a cell of strings, B, such that:

B{1} = 'APR'
B{2} = 'MAY'

Edit: A is a cell and using the function cellstr gives the error

Error using cellstr (line 23)
S must be 2-D. 
5
  • Who is S in your error message? Commented May 1, 2013 at 15:02
  • I have A = data{3,1} (some cell data) so that A is now a <2x6 char>. I then enter B = cellstr(A) and I get the error. Commented May 1, 2013 at 15:09
  • Can you put the data in the question or on pastebin? Commented May 1, 2013 at 15:10
  • Also, does cellstr work for A? Commented May 1, 2013 at 15:12
  • In trying to paste my data I seemed to have found a solution. Initially my data was pairs of months in a <2106x2x6 char>, T say. I then tried cellstr(T(1,:,:)) resulting in the above error. However when I assigned A(:,:) = T(1,:,:) and did cellstr(A), it worked. Commented May 1, 2013 at 15:27

2 Answers 2

9

Use the following function: http://www.mathworks.com/help/matlab/ref/cellstr.html

>> B =  cellstr(A)

B = 

    'APR'
    'MAY'

>> B{1}

ans =

APR
Sign up to request clarification or add additional context in comments.

Comments

2

For a 3D char array T

B = cellstr(T(1,:,:))

Gives the error

Error using cellstr (line 23)
S must be 2-D.

Instead assign it to a 2D matrix first, then use 'cellstr' as Franck suggested above.

A(:,:) = T(1,:,:)
B = cellstr(A)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.