hello everyone, I have the code below , which I want to use for conversion of binary to decimal, but each time I run the program, the software keeps saying >> binary_conversion
Not enough input arguments.
Error in binary_conversion (line 2)
count=numberofdigits(x);
Please I need held on this.
Here is the code:
function decimal_value= binary_conversion(x)
count=numberofdigits(x);
pow=0;
decimal_value=0;
for i=1:1:count
n=mod(x,10);
decimal_value=decimal_value+(n*2^pow);
pow=pow+1;
x=floor(x/10);
end
end

Réponses (1)

Jan
Jan le 24 Mai 2021
Modifié(e) : Jan le 24 Mai 2021

0 votes

How do you try to run the code? Using the green triangle in the editor? Then now input argument is used.
Call it from the command line or from another script or function with an input:
d = binary_conversion(10010101)
A simplification of your code:
function d = binary_conversion(x)
count = numberofdigits(x);
mult = 1;
d = 0;
for i = 1:count
d = d + mod(x, 10) * mult;
mult = mult * 2;
x = floor(x / 10);
end
end

Modifié(e) :

Jan
le 24 Mai 2021

Community Treasure Hunt

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

Start Hunting!

Translated by