Error using bin2dec (in App Designer)
조회 수: 4 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
Walter Roberson
2021년 3월 28일
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!