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
回答(2 个)
Wayne King
2011-10-23
One way:
inputfrac = '12/345';
loc = find(ismember(inputfrac,'/')==1);
num = str2num(inputfrac(1:loc-1));
den = str2num(inputfrac(loc+1:end));
1 个评论
Walter Roberson
2011-10-23
The find is not needed in the above:
[tf, idx] = ismember(inputfrac,'/');
loc = idx(tf);
类别
在 帮助中心 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!