how to create a binary to decimal/decimal to binary function file
Mostra commenti meno recenti
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
Risposta accettata
Più risposte (1)
Jason Moore
il 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
1 Commento
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!