error using function
1 回表示 (過去 30 日間)
古いコメントを表示
% |convert binary string to decimal
% |usage: dec = bin2dec( binary, M )
% |M is the decimal point placement
function dec = bin2decimal( binary, M )
dec = 0;
for i=1:length(binary)
dec = dec + binary(i).*2^(M-i);
end
I tried using this code. But im getting an error.
Undefined function or method 'bin2decimal' for input arguments of type
'double'.
0 件のコメント
回答 (2 件)
Image Analyst
2012 年 4 月 6 日
Can you show the line where you called that function? Did you pass in a double instead of a string for the first argument?
Why does the comment say this: "usage: dec = bin2dec( binary, M )" when the function is called bin2decimal?
Is your bin2decimal function in your calling function's m-file, or is it in its own m-file? If it's in its own m-file, is that file on the path so that it can be found when you try to call it?
0 件のコメント
Andrei Bobrov
2012 年 4 月 6 日
variant
bstr - binary string
function dec = bin2decimal(bstr,M)
% bstr = string array!!!
M = 6;
k = bstr - '0';
dec = k*2.^((numel(k):-1:1)-M).';
eg
bstr = '01001001110';
dec = bin2decimal(bstr,6)
IN your function
function dec = bin2decimal( binary, M )
% binary - string array, eg: '0100110001110'
dec = 0;
for i=1:length(binary)
dec = dec + (binary(i) - '0').*2^(M-i);
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Get Started with MATLAB についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!