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 Comments

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

Sign in to comment.

 Accepted Answer

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 Comments

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!

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!