Cell array Strings help

11 ビュー (過去 30 日間)
tom rawr
tom rawr 2012 年 2 月 26 日
コメント済み: Jan 2017 年 6 月 2 日
Hi, Could I please have some help?
Firstly, typing this into matlab: cellArray1 = {'hi', 'harry', 'potter'} , outputs a 1x3 cell array: cellArray1 =
'hi' 'harry' 'potter'
Now, I want to make a cell array exactly like above but from a String of words such as: text = 'hi harry potter'.
Can anyone please help? I cant figure how to make my text go into a 1x3 cell array :(
Thanks
  2 件のコメント
Thomas Blackwood
Thomas Blackwood 2017 年 6 月 1 日
編集済み: Thomas Blackwood 2017 年 6 月 1 日
Try the strcat function.
If you take your string you can try:
strcat(cellArray1{1}, 32, cellArray1{2}, 32, cellArray1{3})
If you're wondering what the 32 is about check the ASCII table codes.
Jan
Jan 2017 年 6 月 2 日
@Thomas: The question is tricky. tom did not want to concatenate the strings, but to split them.

サインインしてコメントする。

採用された回答

Oleg Komarov
Oleg Komarov 2012 年 2 月 26 日
regexp('hi harry potter',' ','split')
  1 件のコメント
tom rawr
tom rawr 2012 年 2 月 26 日
thanks! So simple, didnt realise!

サインインしてコメントする。

その他の回答 (2 件)

Jan
Jan 2012 年 2 月 26 日
Or:
C = textscan('hi harry potter', '%s')
S = C{1}';
  1 件のコメント
tom rawr
tom rawr 2012 年 2 月 26 日
Thank you Jan!

サインインしてコメントする。


Jan
Jan 2017 年 6 月 2 日
Since Matlab 2013a:
strsplit('hi harray potter')
And modern Matlab versions suggest:
split('hi harray potter')
which creates the new string class.
I'm still astonished that this job concerns the standard problem: Import a text file to a cell string. In R6.5 we needed to create a function to split a string into a cell string by our own. Then regexp('split') was introduced. Later on textread solved this (based on the underlying function dataread, until this was replaced by textscan. Cruel. This is a standard task and it is impossible to write it with backward compatibility.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by