Azzera filtri
Azzera filtri

error using function

4 visualizzazioni (ultimi 30 giorni)
Kugen Raj
Kugen Raj il 6 Apr 2012
% |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'.

Risposte (2)

Image Analyst
Image Analyst il 6 Apr 2012
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 il 6 Apr 2012
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

Categorie

Scopri di più su C4ISR in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by