Simple GUI program performs simple math, but returns two numbers?

I have created a simple MATLAB GUI, consisting of two edit text boxes, and a push button. The first text box is an input space, the button runs code to make a calculation, and the second text box is for outputting the code. The code itself simply multiplies and divides the input by a few numbers (a conversion of sorts I derived on paper), and should output the correct value, but instead it outputs 2 numbers, and I have no idea why. Has anyone else ever had this problem?
Update: The same code works perfectly in a standalone file.

댓글 수: 2

It'll be pretty hard for us to debug anything without your code. Can you attached the code: .m and .fig files?

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

 채택된 답변

Geoff Hayes
Geoff Hayes 2017년 10월 31일
Matt - the problem is that the input text is a string and so you need to convert it into a number before you start to manipulate it
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
input = str2double(get(handles.edit1,'string'));
x = input * 1609.35;
y = x / 3600;
z = y / 0.75;
set(handles.edit2,'string',num2str(x));
Note how we use str2double to do the conversion and then num2str to convert the x to a string that we set in the edit2 text control.
(I suspect that you were entering in a two digit number which, without the conversion, is a 1x2 char array. When you multiply, you get two numbers that you would then display in the edit control.)

댓글 수: 2

Also, look at the output. Do you mean return x or z?
%set(handles.edit2,'string',x); %Do you mean z???
%set(handles.edit2,'string',num2str(z)); %< HERE! use num2str(z)
Yes I meant z, I was testing to see where it broke and forgot to change it back before testing. Thanks!

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

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기

태그

질문:

2017년 10월 31일

댓글:

2017년 10월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by