Effacer les filtres
Effacer les filtres

error using function

1 vue (au cours des 30 derniers jours)
Kugen Raj
Kugen Raj le 6 Avr 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'.

Réponses (2)

Image Analyst
Image Analyst le 6 Avr 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 le 6 Avr 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

Catégories

En savoir plus sur C4ISR dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by