How to convert string within a cell array into an array?

I have a cell array like this:
c = {'[18,1]' '[18,2]' '[18,3]' '[19,4]'}
I would like to be able to convert it into something like this:
A = [18,1] [18,2] [18,3] [19,4]
Essentially what I want to do is to separate the strings in the cell arrays and turn them into individual array, can someone please help?

 採用された回答

Stephen23
Stephen23 2017 年 9 月 10 日
編集済み: Stephen23 2017 年 9 月 10 日

0 投票

Method one: concatenation and sscanf:
>> c = {'[18,1]' '[18,2]' '[18,3]' '[19,4]'};
>> reshape(sscanf([c{:}],'[%f,%f]'),2,[]).'
ans =
18 1
18 2
18 3
19 4
Method two: cellfun and cell2mat:
>> A = cell2mat(cellfun(@(s)sscanf(s,'[%f,%f]'),c,'uni',0)).'
A =
18 1
18 2
18 3
19 4
However if you are importing these strings from a file it would be easier to import the data correctly as numeric in the first place. Where do these strings come from?

1 件のコメント

Regis Kan
Regis Kan 2017 年 9 月 10 日
編集済み: Regis Kan 2017 年 9 月 10 日
Yeah, thanks. The numbers does not originate from files, got them using shortestpath function.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

質問済み:

2017 年 9 月 10 日

編集済み:

2017 年 9 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by