how to create a binary to decimal/decimal to binary function file
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Stephen23
2015-2-7
编辑:Stephen23
2015-2-7
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 个评论
Stephen23
2018-10-11
编辑:Stephen23
2018-10-11
@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:
更多回答(1 个)
Jason Moore
2015-2-7
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
1 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!