필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Max
Max 2015년 2월 7일
편집: Stephen23 2018년 10월 11일
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

채택된 답변

Stephen23
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
CK Yeap
CK Yeap 2018년 10월 11일
Hi Stephen, may I know how to input the variable on the function?
Stephen23
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
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
Stephen23
Stephen23 2015년 2월 7일
You should avoid naming the input variable input, as this shadows the inbuilt function input .

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by