Convert a single char array to a vector of doubles with a non-whitespace delimiter [R2017b, Windows7]

15 ビュー (過去 30 日間)
Hi,
I have a binary file that when I load up into Matlab produces the following cell which contains a cell array called data:
1x1 cell array:
{'44.565,433.4544,34.332'}
This can be a very long list of numbers and I would like to avoid using str2num (which works fine). I have tried using:
slope = sprintf('%s,',data{:})
doubleArray = sscanf(slope, '%f')
This only give me the first number. I suspect this is because I have a ',' as a delimiter and not a whitespace character.
str2double(data)
returns a NaN and I suspect this is because I have a single char array in a cell and not an array of strings.
Any advice as to how to do this without str2num?
Cheers, TJ
  1 件のコメント
Stephen23
Stephen23 2018 年 1 月 4 日
Using sscanf is most efficient:
sscanf(a{1}, '%f,')

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

採用された回答

Birdman
Birdman 2018 年 1 月 4 日
編集済み: Birdman 2018 年 1 月 4 日
a={'44.565,433.4544,34.332'};
str=a{1}(:);
strNum=str2double(strsplit(str.',','))
Thanks to Stephen Cobeldick, another and faster approach:
sscanf(a{1}, '%f,')
  2 件のコメント
Birdman
Birdman 2018 年 1 月 4 日
My pleasure and you had multiple options, perfect!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by