Transform char variable to matrix

17 次查看(过去 30 天)
Hi, I have a cell variable that looks like this:
I'D like to know if there is a way to transform it into a matrix of this kind:
NewVar=[2 5; 2 3; 2 5];
Thanks

采纳的回答

Walter Roberson
Walter Roberson 2020-2-14
tmp = {'002,005';'002,003';'002,005'};
NewVar = cell2mat(cellfun(@(S) sscanf(S, '%f,%f').', tmp, 'uniform', 0));

更多回答(2 个)

Stephen23
Stephen23 2020-2-15
Efficient solution:
>> C = {'002,005';'002,003';'002,005'};
>> sscanf(sprintf('%s;',C{:}),'%f,%f;',[2,Inf]).'
ans =
2 5
2 3
2 5

Sindar
Sindar 2020-2-14
tmp={'002,005';'002,003';'002,005'};
NewVar=str2double(split(tmp,','))
  2 个评论
Guido Pozzi
Guido Pozzi 2020-2-14
When I run this I get 'Undefined function 'split' for input arguments of type 'cell'. I'm using Matlab 2015, maybe that split function isn't available for my matlab version.
Sindar
Sindar 2020-2-14
strsplit might work, but you might need to loop over cells

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by