Transform char variable to matrix

17 Ansichten (letzte 30 Tage)
Guido Pozzi
Guido Pozzi am 14 Feb. 2020
Beantwortet: Stephen23 am 15 Feb. 2020
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

Akzeptierte Antwort

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

Weitere Antworten (2)

Stephen23
Stephen23 am 15 Feb. 2020
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 am 14 Feb. 2020
tmp={'002,005';'002,003';'002,005'};
NewVar=str2double(split(tmp,','))
  2 Kommentare
Guido Pozzi
Guido Pozzi am 14 Feb. 2020
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 am 14 Feb. 2020
strsplit might work, but you might need to loop over cells

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by