Transform char variable to matrix

조회 수: 17 (최근 30일)
Guido Pozzi
Guido Pozzi 2020년 2월 14일
답변: Stephen23 2020년 2월 15일
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