i have this output:
output = dial ('1FUNDOG4YOU')
output =
1 3 8 6 3 6 4 4 9 6 8
(1 X 11) cell
but this is what i want
output :13863644968
(1X 1)
I've tried all sort of ridiculous stuff, str2num and the likes but nothing works . any idea on what to do to make the conversion happen? thanks

 採用された回答

Stephen23
Stephen23 2016 年 6 月 14 日
編集済み: Stephen23 2016 年 6 月 14 日

1 投票

If the elements of the cell array are numeric scalars:
>> C = {1,3,8,6,3,6,4,4,9,6,8};
>> sprintf('%d',C{:})
ans = 13863644968
Or if they are strings:
>> C = {'1','3','8','6','3','6','4','4','9','6','8'};
>> sprintf('%s',C{:})
ans = 13863644968
Note that this string has size 1x11 (strings are not size 1x1 in MATLAB). If you need it to be stored in a 1x1 array, then you can put it inside a cell.

3 件のコメント

OLUBUKOLA ogunsola
OLUBUKOLA ogunsola 2016 年 6 月 14 日
works but its comes out as char , but my objective is to get a uint64 class. when i use uint64(ans), it gives me a 1X11
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 6 月 14 日
C = {1,3,8,6,3,6,4,4,9,6,8}
C=sprintf('%d',C{:})
C=uint64(str2num(C))
OLUBUKOLA ogunsola
OLUBUKOLA ogunsola 2016 年 6 月 14 日
編集済み: OLUBUKOLA ogunsola 2016 年 6 月 14 日
perfect ! thanks

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 6 月 14 日

1 投票

Be careful, str2num() will create double precision with only 53 bits of precision. If you need the full 64 bits of precision you will need a different calculation.
C = {1,3,8,6,3,6,4,4,9,6,8};
Cv = uint64([C{:}]);
output = sum( Cv .* (uint64(10).^uint64(length(Cv)-1:-1:0)), 'native' );

6 件のコメント

OLUBUKOLA ogunsola
OLUBUKOLA ogunsola 2016 年 6 月 15 日
it actually works , problem now is testing for conditions. input should be only numbers and letters . I've tried ~isnumber , if ~isletter , isstrprop but nothing works
Stephen23
Stephen23 2016 年 6 月 15 日
編集済み: Stephen23 2016 年 6 月 15 日
@OLUBUKOLA ogunsola: read the isstrprop documentation:
>> inp = '1FUNDOG4YOU';
>> all(isstrprop(inp,'alphanum'))
ans = 1
>> inp = '!%()="';
>> all(isstrprop(inp,'alphanum'))
ans = 0
And read about assert.
OLUBUKOLA ogunsola
OLUBUKOLA ogunsola 2016 年 6 月 15 日
thanks for the info . since all(isstrprop(inp,'alphanum'))returns ans=1 or 0, i did this
x=all(isstrprop(inp,'alphanum'))
then
if x=1
the code else
output =0
but it still doesnt work. Debugging shows that the program continues with the program even when x==0. maybe its something in my code that is causing that. meanwhile I'm studying the assert ,hopefully that will work
Stephen23
Stephen23 2016 年 6 月 15 日
編集済み: Stephen23 2016 年 6 月 15 日
The function isstrprop and all both return logical values, so you don't need to compare them to anything. They are already boolean values, that can be used directly in an if statement:
if all(isstrprop(...))
...
else
...
end
No testing for "==0" is required.
OLUBUKOLA ogunsola
OLUBUKOLA ogunsola 2016 年 6 月 15 日
problem with assert is it outputs an error , i don't want that i want output =0 . so i wrote the code this way
assert(isa(testStr,'alphanum'),'output=0.') but gets an error :
Error using dial (line 6) output=0
OLUBUKOLA ogunsola
OLUBUKOLA ogunsola 2016 年 6 月 15 日
編集済み: Walter Roberson 2016 年 6 月 15 日
this is the code i wrote :
function output = dial (testStr)
c = containers.Map({'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}, ...
[0,1,2,3,4,5,6,7,8,9,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9]) ;
testOut = NaN(1,numel(testStr)) ;
x=all(isstrprop('testStr','alphanum'));
if x==1
for ii = 1:numel(testStr);
testOut(ii) = c(strcat(testStr(ii)));
C=sprintf('%d',testOut(:));
C=uint64(str2double(C));
end
output=C ;
else
output=0;
end
end

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by