error using function
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
% |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 Commenti
Risposte (2)
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?
0 Commenti
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
0 Commenti
Vedere anche
Categorie
Scopri di più su Get Started with MATLAB in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!