0

I have the following string (char of 1X48) in cell in Matlab

{ {1 , 0 , 0 } , { 0 , 1 , 0 } , { 0 , 0 , 1 } }.

I am trying to get three separate strings in new line with just space, so that data will look like

1 0 0

0 1 0

0 0 1

I will really appreciate if anyone has any idea to covert in matlab.

Thanks

2 Answers 2

1

Better to use cell2mat function

In your case you can try something like this,

temp = { {1 , 0 , 0 } , { 0 , 1 , 0 } , { 0 , 0 , 1 } }; out = [cell2mat(temp{1, 1}); cell2mat(temp{1, 2}); cell2mat(temp{1, 3})]

I hope it will help!!

3
  • Thanks! This is really help me. herohuyongtao also suggested to use strjoin fuction to get the oupput in string. Feb 9, 2014 at 15:40
  • I am having issue with reading string from cell in matlab. I have excel spreadsheet which I am reading in matlab using xlsread. I have tried using eval() to convert above string to cell but somehow it is not breaking into individual cell. I cann't perform the method you have mentioned. Do you know what might be the issue? Feb 10, 2014 at 2:10
  • It would be great if you can please share any sample data of excel datasheet but in general to convert a "string to cell" better to use str2mat and mat2cell Feb 10, 2014 at 5:11
0

You are looking for strjoin:

a = { {1 , 0 , 0 } , { 0 , 1 , 0 } , { 0 , 0 , 1 } };
b = strjoin(a, ' ')

Then:

b =

1 0 0 0 1 0 0 0 1

Edit: if you want to get new lines involved, you can use

b = [strjoin(a(1), ' '); strjoin(a(2), ' '); strjoin(a(3), ' ');]

Then:

b =

1 0 0
0 1 0
0 0 1

P.S.: strjoin works in MATLAB R2013b. For earlier versions, you can download the strjoin function from here.

1
  • Thanks! This is also help me. This is working as well. Feb 9, 2014 at 15:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.