converting a specific string to two fractions

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

Respuestas (2)

Wayne King
Wayne King el 23 de Oct. de 2011
One way:
inputfrac = '12/345';
loc = find(ismember(inputfrac,'/')==1);
num = str2num(inputfrac(1:loc-1));
den = str2num(inputfrac(loc+1:end));

1 comentario

Walter Roberson
Walter Roberson el 23 de Oct. de 2011
The find is not needed in the above:
[tf, idx] = ismember(inputfrac,'/');
loc = idx(tf);

Iniciar sesión para comentar.

Andrei Bobrov
Andrei Bobrov el 23 de Oct. de 2011
numden = cellmat(@str2num,regexp( '12/345' ,'/', 'split'))

1 comentario

Benjamin Isetta
Benjamin Isetta el 23 de Oct. de 2011
I like this one. completely different than the way I made mine.

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Preguntada:

el 23 de Oct. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by