converting a specific string to two fractions
Ältere Kommentare anzeigen
I'm trying to write a function that takes as input a string in the form of '12/345' and would return two integers:
numerator = 12
denominator = 345
Antworten (2)
Wayne King
am 23 Okt. 2011
One way:
inputfrac = '12/345';
loc = find(ismember(inputfrac,'/')==1);
num = str2num(inputfrac(1:loc-1));
den = str2num(inputfrac(loc+1:end));
1 Kommentar
Walter Roberson
am 23 Okt. 2011
The find is not needed in the above:
[tf, idx] = ismember(inputfrac,'/');
loc = idx(tf);
Andrei Bobrov
am 23 Okt. 2011
numden = cellmat(@str2num,regexp( '12/345' ,'/', 'split'))
1 Kommentar
Benjamin Isetta
am 23 Okt. 2011
Kategorien
Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!