how to convert a string to a vector

162 ビュー (過去 30 日間)
lamghari
lamghari 2015 年 12 月 1 日
コメント済み: Walter Roberson 2015 年 12 月 8 日
Hi I have a string chain and wish to convert it into a vector So if my input is: x =1,3,3,4,5,6,6 I need an output
y = [1 3 3 4 5 6 6] How do I do this?

回答 (1 件)

per isakson
per isakson 2015 年 12 月 1 日
編集済み: per isakson 2015 年 12 月 1 日
One way
>> str ='1,3,3,4,5,6,6'
str =
1,3,3,4,5,6,6
>> num = textscan( str, '%f', 'Delimiter',',' )
num =
[7x1 double]
and together with the missing step
str ='1,3,3,4,5,6,6';
num = textscan( str, '%f', 'Delimiter',',' );
num = permute( num{1}, [2,1] )
num =
1 3 3 4 5 6 6
another way
>> str2num( str )
ans =
1 3 3 4 5 6 6
  7 件のコメント
lamghari
lamghari 2015 年 12 月 8 日
ok.thank you very much
Walter Roberson
Walter Roberson 2015 年 12 月 8 日
output = ['y = [', regexprep(num, ',', ' '), ']' ];
If what you want is the string 'y = [6 7 6 6 6 6 6 5 4 4 4 4 ... ]'

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by