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

2 views (last 30 days)
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

Accepted Answer

Stephen23
Stephen23 on 7 Feb 2015
Edited: Stephen23 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.
Try this code instead:
function out = binXdec(val)
fprintf('Available options:\t 1 = decimal to binary \t 2 = binary to decimal\n')
switch input('Please select your option: ','s')
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
Please select your option: 1
ans =
1111011
  3 Comments
CK Yeap
CK Yeap on 11 Oct 2018
Hi Stephen, may I know how to input the variable on the function?
Stephen23
Stephen23 on 11 Oct 2018
Edited: Stephen23 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:

Sign in to comment.

More Answers (1)

Jason Moore
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

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!