# how to create a binary to decimal/decimal to binary function file

34 views (last 30 days)
Max on 7 Feb 2015
Edited: Stephen on 11 Oct 2018
hello,
I am a little new to writing matlab function files.
I need to write a function file that, takes in either a binary or decimal and converts it to either decimal or binary respectively. The program should first ask whether to convert to binary or decimal.
I know that matlab already has the dec2bin, bin2dec commands so how can i go about making a function file that will first ask what I am converting to and then proceed to make the proper conversion?
All comments, thoughts, ideas, pointers, and any thought provoking ideas will be greatly appreciated!
Thank You

Stephen on 7 Feb 2015
Edited: Stephen on 7 Feb 2015
You made several mistakes in your code, lets have a look through it. Firstly you define the function with
function output = cenversion(input)
But notice how you specify the input argument name input. This is immediately going to cause you problems, because this is also the name of the inbuilt input function, which you also want to use. You should never shadow inbuilt functions or variables like this: avoid naming any of your own variables or functions with the same names as inbuilt ones.
Then you try to check the user-selection with isa(input,'1'), but when you read the documentation for isa , this syntax is nowhere to be found. isa check for a particular class, and '1' is not a known class name, so this is never going to work.
function out = binXdec(val)
fprintf('Available options:\t 1 = decimal to binary \t 2 = binary to decimal\n')
case '1'
out = dec2bin(val);
case '2'
out = bin2dec(val);
end
end
Note how I used the more robust syntax for input, with the second optional 's' argument, and did not shadow any inbuilt functions or variables, and used the simpler fprintf(...) rather than disp(sprintf(...)) When I save this and run it, I get this in my command window:
>> binXdec(123)
Available options: 1 = decimal to binary 2 = binary to decimal
ans =
1111011
Stephen on 11 Oct 2018
@CK Yeap: as I showed in my last comment, the simplest way is to write it in the command line:
>> binXdec(1234)
and then press enter. For this to work you will need to have saved that function somewhere on the MATLAB Search Path (e.g. in the current directory):
How to call functions with input/output arguments is explained in the introductory tutorials:

Jason Moore on 7 Feb 2015
Since bin2dec returns a string you could use the isa command to see if the value is a string or a numeric command. In the following example I check to see if the input to the function is a numeric, and if it is I do bin2dec. If it is not a numberic, I assume a string with a binary number has been passed in and use bin2dec.
function output = switchNumType(input)
if isa(input,'numeric')
output = dec2bin(input);
else
output = bin2dec(input);
end
Stephen on 7 Feb 2015
You should avoid naming the input variable input, as this shadows the inbuilt function input .