Why is my code seemingly ignoring some of the for loop commands?
Hello, I’m new to MATLAB and I’m trying to create a user-defined function that acts like bin2dec using manual conversion of binary to decimal integer, but I can’t figure out why I’m getting outputs of "1" or no output value for x altogether. Can someone please help me understand what’s wrong with my code? Thank you!!
% mybin2dec takes one single valued text input argument representation of a binary number and converts it to a decimal integer
function b=mybin2dec(x)
x=input(‘Enter a binary number as a text input: ‘, ‘s’)
str2num(x)
n=length(x)-1
for y=[1:length(x)]
if x(y)==49
d=(1*(2^n))
n=n-1
elseif x(y)==48
d=(0*(2^n))
n=n-1
end
end
sum(d)Hello, I’m new to MATLAB and I’m trying to create a user-defined function that acts like bin2dec using manual conversion of binary to decimal integer, but I can’t figure out why I’m getting outputs of "1" or no output value for x altogether. Can someone please help me understand what’s wrong with my code? Thank you!!
% mybin2dec takes one single valued text input argument representation of a binary number and converts it to a decimal integer
function b=mybin2dec(x)
x=input(‘Enter a binary number as a text input: ‘, ‘s’)
str2num(x)
n=length(x)-1
for y=[1:length(x)]
if x(y)==49
d=(1*(2^n))
n=n-1
elseif x(y)==48
d=(0*(2^n))
n=n-1
end
end
sum(d) Hello, I’m new to MATLAB and I’m trying to create a user-defined function that acts like bin2dec using manual conversion of binary to decimal integer, but I can’t figure out why I’m getting outputs of "1" or no output value for x altogether. Can someone please help me understand what’s wrong with my code? Thank you!!
% mybin2dec takes one single valued text input argument representation of a binary number and converts it to a decimal integer
function b=mybin2dec(x)
x=input(‘Enter a binary number as a text input: ‘, ‘s’)
str2num(x)
n=length(x)-1
for y=[1:length(x)]
if x(y)==49
d=(1*(2^n))
n=n-1
elseif x(y)==48
d=(0*(2^n))
n=n-1
end
end
sum(d) for loop, bin2dec MATLAB Answers — New Questions