フィルターのクリア

error using function

4 ビュー (過去 30 日間)
Kugen Raj
Kugen Raj 2012 年 4 月 6 日
% |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'.

回答 (2 件)

Image Analyst
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?

Andrei Bobrov
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

カテゴリ

Help Center および File ExchangeC4ISR についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by