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

bintointhextointhomeworkwhile loop

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?

Best 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.')