Error using bin2dec (in App Designer)

3 visualizzazioni (ultimi 30 giorni)
Brian J
Brian J il 27 Mar 2021
Risposto: Walter Roberson il 28 Mar 2021
Hello, I have to do a program to convert decimal to binary, octal and hexamdecimal and vice versa, when I did the decimal to binary conversion everything went well, but when I convert from decimal to binary I have problems.
Every time I enter a decimal number in the EditField, it throws me this message: "Input argument must be a character vector, string, or cell array of character vectors."
I have understood that in the EditField I have to convert the values that I enter into a vector (so to speak)
So as I do when I enter binary numbers in the EditField it returns me the result in decimal.
function ConvertirButtonPushed(app, event)
% De Decimal a los otros
if(app.DecimalButton.Value)
% Example - Decimal to Binary
result_2 = dec2bin(app.IngreseunnumeroEditField.Value);
app.BinarioTextArea.Value = num2str(result_2);
end
% Error - Binary to Decimal
if(app.BinarioButton.Value)
result_1 = bin2dec(app.IngreseunnumeroEditField.Value);
app.DecimalTextArea.Value = num2str(result_1);
end
end

Risposte (1)

Walter Roberson
Walter Roberson il 28 Mar 2021
result_2 = dec2bin(app.IngreseunnumeroEditField.Value);
You say that works, but for that to work, IngreseunnumeroEditField must be returning a numeric value not text
result_1 = bin2dec(app.IngreseunnumeroEditField.Value);
The exact same field is being used, but for bin2dec to function it must be passed a character vector, but we established above it is numeric instead.
You need to make the edit field text instead of numeric, and for the dec2bin case, str2double the text before passing it in.
Do not take the route of instead converting the numeric value to text to pass into bin2dec: if you do that then you will fail if the user enters more than 16 bits.

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by