Stuck on while loop, and cant get script to call out answer from function.

fprintf('This program will convert numbers between hexadecimal,\n');
fprintf('binary, and integers. Choose one of the below options:\n\n');
fprintf('1-Convert Binary to Integer\n');
fprintf('2-Convert Hexadecimal to Integer\n');
fprintf('X-Exit the program\n');
fprintf('\n');
%------------------------------------------------------------------------%
myoption = 0;
myInt = 0;
myHex = '00';
myBinary = '00000000';
%------------------------------------------------------------------------%
myoption = input('Enter an option: \n', 's');
while myoption ~= 'X'
if str2double(myoption) == 1
run myBinToInt;
else str2double(myoption) == 2
run myHexToInt
end
end
fprintf('The Binary number%s, converted to integer is: \n', myBinary);
fprintf('Integer = %d\n', myInt);
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
That is my script i want it to give me the binarynumber from the function and also to quit once it is done.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function myBinToInt(str) %Function to convert Binary number to Integer.
myBinary = input ('Enter a binary number: ', 's');
myInt = 0; %Initializes the int to zero.
i =1; %Initializes the i variable to 1.
while i < length(myBinary) + 1
myInt = myInt + str2double(myBinary(i)) * 2^(length(myBinary) - i);
i = i + 1;
end
end
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
This is the function.
I dont know what im doing wrong ive been trying for over 4 hours any help would be appreciated. Also, anyone know how i could get 'X' and 'x' to quit the program?

3 Comments

ok so i removed the while loop it looks like this now. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
fprintf('This program will convert numbers between hexadecimal,\n');
fprintf('binary, and integers. Choose one of the below options:\n\n');
fprintf('1-Convert Binary to Integer\n');
fprintf('2-Convert Hexadecimal to Integer\n');
fprintf('X-Exit the program\n');
fprintf('\n');
%------------------------------------------------------------------------%
myoption = 0;
myInt = 0;
myHex = '00';
myBinary = '00000000';
%------------------------------------------------------------------------%
myoption = input('Enter an option: \n', 's');
if str2double(myoption) == 1
run myBinToInt;
elseif str2double(myoption) == 2
run myHexToInt
elseif myoption == 'X' | myoption == 'x'
disp('The program will now quit.')
else
run Convert
end
how do i call out the myBinary from the function through the script?
Your code is currently very difficult to read. Please learn to format your code correctly on this forum. You can use the {} Code button that you will find above the text box. Also very useful is the preview panel underneath the text box, which shows what you text will look like.
Please edit your question and comment and format the code correctly.

Sign in to comment.

 Accepted Answer

CokePQ - it seems that you are correctly able to call your myBinToInt function from your script (thought you don't need to invoke run before the function name) so could just have
myoption = input('Enter an option: \n', 's');
if str2double(myoption) == 1
myBinToInt;
elseif str2double(myoption) == 2
myHexToInt;
elseif myoption == 'X' | myoption == 'x'
disp('The program will now quit.')
else
% run Convert
end
If you want to have your myBinToInt function display the answer (of the binary to integer conversion), then just use fprintf to write the answer to the console. The following could be added after the code exits the while loop
fprintf('The conversion of the binary string %s to an integer is %d\n',myBinary,myInt);
Note that your function signature for myBinToInt includes a str input parameter which is unused (so this could be removed).
One of your other questions was how to exit the outer program if the user enters 'X' or 'x'. Just use strcmpi to check for this (the function is case insensitive)
elseif strcmpi(myoption'x') == 1
disp('The program will now quit.')

24 Comments

Thank You for the help the script looks like this:
fprintf('This program will convert numbers between hexadecimal,\n');
fprintf('binary, and integers. Choose one of the below options:\n\n');
fprintf('1-Convert Binary to Integer\n');
fprintf('2-Convert Hexadecimal to Integer\n');
fprintf('X-Exit the program\n');
fprintf('\n');
%------------------------------------------------------------------------%
myoption = 0;
myInt = 0;
myHex = '00';
myBinary = '00000000';
%------------------------------------------------------------------------%
myoption = input('Enter an option: \n', 's');
if str2double(myoption) == 1
myBinToInt;
elseif str2double(myoption) == 2
myHexToInt
elseif myoption == 'X' | myoption == 'x'
disp('The program will now quit.')
else
Convert
end
%-----------------------------------------------------------------------%
fprintf('The Binary number %s, converted to integer is %d\n', myBinary, myInt);
The problem now is that instead of using myBinary from the function it uses myBinary from script so it prints out 000000000 and 00, for myBinary and myint respectively. if i change the name from myBinary to myBinary1 it says it is undefined variable. What else can i try?
this is my function:
function myBinToInt %Function to convert Binary number to Integer.
myBinary = input ('Enter a binary number: ', 's'); %Stores user input as myBinToInt.
myInt = 0; %Initializes the int to zero.
i =1; %Initializes the i variable to 1.
while i < length(myBinary) + 1 %Runs a while loop until the length of loop is met.
myInt = myInt + str2double(myBinary(i)) * 2^(length(myBinary) - i); %Converts the Bin to Int.
i = i + 1; %Counts number of loops.
end
end
You will need to modify your function signature so that it returns the binary number and the integer equivalent. Something like
function [myBinary, myInt] = myBinToInt
% etc.
Then, in your script, you would call this function as
if str2double(myoption) == 1
[myBinary, myInt] = myBinToInt;
fprintf('The Binary number %s, converted to integer is %d\n', myBinary, myInt);
elseif str2double(myoption) == 2
% etc.
Note that the fprintf is moved to the if body since for the other cases (conversion from hex to int) you will not want to print out the binary statement (as it makes no sense).
Thank you Geoff it worked. Can you give me a hint how i can check whether the input from the function is a binary number or not? i tried
noBin = 0;
noBin = str2double(1) * str2double(2);
if noBin == 1 || noBin ==0
while
etc....
else
disp('Enter Valid Binary number')
myfunction
end
since all the numbers must be 1 or 0 if i multiply them its zero, but how can i check for all the elements in the string?
ok i think i got it. my function is:
function [myBinary, myInt] = myBinToInt %Function to convert Binary number to Integer.
i =1; %Initializes the i variable to 1.
myInt = 0;
noBin = 0;
%----------------------------------------------------------------------%
myBinary = input ('Enter a binary number: ', 's'); %Stores user input as myBinToInt.
noBin = str2double(myBinary(1)) * str2double(myBinary(length(myBinary)-1));
if noBin == 1 || noBin == 0
while i < length(myBinary) + 1 %Runs a while loop until the length of loop is met.
myInt = myInt + str2double(myBinary(i)) * 2^(length(myBinary) - i); %Converts the Bin to Int.
i = i + 1; %Counts number of loops.
end
else
disp('Enter valid Binary number')
myBinToInt
end
never mind it still runs for certain values
it works well until i input 11112 because i get a zero
Use regexp to check for existence of numbers that aren't 0 or 1. For example,
regexp('1111','[^01]')
returns an empty matrix, whereas
regexp('11112','[^01]')
returns 5.
sorry i forgot to mention i cant use functions i havent used in class. im tryin to do it with a for loop but still not quite there. Thank you for the help i really appreciate it.
And we're supposed to know what functions you used in class? Please list them.
for loops, while loops, if loops, must convert the long way not short cuts, pretty much the basic stuff. %d,%s, str2num, str2double, floor, mod, strcat. thats pretty much it.
CokePQ - since this is homework, then you must flag your question with the homework tag.
As for checking whether the input string has a character that is neither a '0' nor a '1', then use a for loop to iterate over each element of the string.
ok got it Geoff thank you for the help bro.
Can i have some help in this function? ive tried for the last couple hours cant get it to work some advice would be nice.
function [myHex, myInt] = myHexToInt
myInt = 0;
i = 1;
hexnum = 0;
%-------------------------------------------------------------------------%
myHex = input('Enter a Hex number: \n', 's');
for c = 1 : length(myHex)
if myHex == '1'
hexnum = 1;
elseif myHex == '2'
hexnum = 2;
elseif myHex == '3'
hexnum = 3;
elseif myHex == '4'
hexnum =4;
elseif myHex == '5'
hexnum = 5;
elseif myHex == '6'
hexnum = 6;
elseif myHex == '7'
hexnum = 7;
elseif myHex == '8'
hexnum = 8;
elseif myHex == '9'
hexnum = 9;
elseif myHex == 'A' | myHex == 'a'
hexnum = 10;
elseif myHex == 'B' | myHex == 'b'
hexnum = 11;
elseif myHex == 'C' | myHex == 'c'
hexnum = 12;
elseif myHex == 'D' | myHex == 'd'
hexnum = 13;
elseif myHex == 'E' | myHex == 'e'
hexnum = 14;
elseif myHex == 'F' | myHex == 'f'
hexnum = 15;
else
disp('Enter valid Hex number')
myHexToInt
end
end
while i < length(myHex) + 1
myInt = myInt + hexnum * 16^(length(myHex) - i);
i = i + 1;
end
hexnum
myInt
end
The math is wrong and it doesnt change the hexnumber to corresponding value.
Your code iterates over c, from 1 to the length of myHex, but no where does it make use of c. I suspect that at each iteration of the loop you should access the cth element from myHex and use that in your if statement checks.
As I mentioned before, use strcmpi for when you want to compare the cth element of myHex with (for example) A or a (it is much simpler/cleaner).
Why not just use hex2num ? MATLAB already does this conversion quickly and neatly, and it makes your function much neater, faster and robust:
function [myHex, myInt] = myHexToInt
% Prompt User to input a Hexadecimal number, conver this to integer.
myHex = input('Enter a Hex number: \n', 's');
myInt = hex2dec(myHex);
end
And you can call it in the command window like this:
>> [A,B] = myHexToInt
Enter a Hex number:
2d
A = 2d
B = 45
have to do it the long way for class.
ok this is my function now:
function [myHex, myInt] = myHexToInt
myInt = 0;
i = 1;
hexnum = 0;
%-------------------------------------------------------------------------%
myHex = input('Enter a Hex number: \n', 's');
for c = 1 : length(myHex)
if myHex(c) == '1';
hexnum = 1;
elseif myHex(c) == '2';
hexnum = 2;
elseif myHex(c) == '3';
hexnum = 3;
elseif myHex(c) == '4';
hexnum = 4;
elseif myHex(c) == '5';
hexnum = 5;
elseif myHex(c) == '6';
hexnum = 6;
elseif myHex(c) == '7';
hexnum = 7;
elseif myHex(c) == '8';
hexnum = 8;
elseif myHex(c) == '9';
hexnum = 9;
elseif myHex(c) == 'A' | myHex(i) == 'a'
hexnum = 10;
elseif myHex(c) == 'B' | myHex(i) == 'b'
hexnum = 11;
elseif myHex(c) == 'C' | myHex(i) == 'c'
hexnum = 12;
elseif myHex(c) == 'D' | myHex(i) == 'd'
hexnum = 13;
elseif myHex(c) == 'E' | myHex(i) == 'e'
hexnum = 14;
elseif myHex(c) == 'F' | myHex(i) == 'f'
hexnum = 15;
else
disp('Enter valid Hex number')
myHexToInt
end
end
while i < length(myHex) + 1
%runs a while loop until the length of myBinary is met
myInt = myInt + str2double(hexnum(i)) * 16^(length(myHex) - i);
%Converts the Bin to Int.
i = i + 1; %Counts number of loops.
end
hexnum
myInt
end
im getting an error that says that when attempting to access hexnum(2) the index is out of bounds. Any hints?
I tried to use the strcmpi function but it gave an error.
the math doesn't work out either.
ok i got it to read for more variables but my math is still wrong. can anyone see my mistake i get 170 for 1A its supposed to be 26.
function [myHex, myInt] = myHexToInt
myInt = 0;
hexnum = 0;
i = 1;
%-------------------------------------------------------------------------%
myHex = input('Enter a Hex number: \n', 's');
for c = 1 : length(myHex)
if myHex(c) == '1';
hexnum(c) = 1;
elseif myHex(c) == '2';
hexnum(c) = 2;
elseif myHex(c) == '3';
hexnum(c) = 3;
elseif myHex(c) == '4';
hexnum(c) = 4;
elseif myHex(c) == '5';
hexnum(c) = 5;
elseif myHex(c) == '6';
hexnum(c) = 6;
elseif myHex(c) == '7';
hexnum(c) = 7;
elseif myHex(c) == '8';
hexnum(c) = 8;
elseif myHex(c) == '9';
hexnum(c) = 9;
elseif myHex(c) == 'A' | myHex(c) == 'a'
hexnum(c) = 10;
elseif myHex(c) == 'B' | myHex(c) == 'b'
hexnum(c) = 11;
elseif myHex(c) == 'C' | myHex(c) == 'c'
hexnum(c) = 12;
elseif myHex(c) == 'D' | myHex(c) == 'd'
hexnum(c) = 13;
elseif myHex(c) == 'E' | myHex(c) == 'e'
hexnum(c) = 14;
elseif myHex(c) == 'F' | myHex(c) == 'f'
hexnum(c) = 15;
else
disp('Enter valid Hex number')
myHexToInt
end
end
while i < length(myHex) + 1
%runs a while loop until the length of myBinary is met
myInt = myInt + hexnum(c) * 16^(length(myHex) - i);
%Converts the Bin to Int.
i = i + 1; %Counts number of loops.
end
hexnum
myInt
end
it worked like this:
function [myHex, myInt] = myHexToInt
myInt = 0;
hexnum = 0;
i = 1;
%-------------------------------------------------------------------------%
myHex = input('Enter a Hex number: \n', 's');
for c = 1 : length(myHex)
if myHex(c) == '1';
hexnum(c) = 1;
elseif myHex(c) == '2';
hexnum(c) = 2;
elseif myHex(c) == '3';
hexnum(c) = 3;
elseif myHex(c) == '4';
hexnum(c) = 4;
elseif myHex(c) == '5';
hexnum(c) = 5;
elseif myHex(c) == '6';
hexnum(c) = 6;
elseif myHex(c) == '7';
hexnum(c) = 7;
elseif myHex(c) == '8';
hexnum(c) = 8;
elseif myHex(c) == '9';
hexnum(c) = 9;
elseif myHex(c) == 'A' | myHex(c) == 'a'
hexnum(c) = 10;
elseif myHex(c) == 'B' | myHex(c) == 'b'
hexnum(c) = 11;
elseif myHex(c) == 'C' | myHex(c) == 'c'
hexnum(c) = 12;
elseif myHex(c) == 'D' | myHex(c) == 'd'
hexnum(c) = 13;
elseif myHex(c) == 'E' | myHex(c) == 'e'
hexnum(c) = 14;
elseif myHex(c) == 'F' | myHex(c) == 'f'
hexnum(c) = 15;
else
disp('Enter valid Hex number')
myHexToInt
end
end
while i < length(myHex) + 1
%runs a while loop until the length of myBinary is met
myInt = myInt + hexnum(i) * 16^(length(myHex) - i);
%Converts the Bin to Int.
i = i + 1; %Counts number of loops.
end
hexnum
myInt
end
i got everything to work thank you so much Geoff i owe you bro.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 15 Feb 2015

Commented:

on 17 Feb 2015

Community Treasure Hunt

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

Start Hunting!