Liquidmetal
Liquidmetal

Reputation: 295

Matlab GUI, use an input for a static text box and convert it with a basic push button

I am struggling with how the Matlab gui interface works.

I am not looking for an answer, only some more guidance of how to do this.

I am trying to convert a temperature in an edit box from F to C...so I think I need the equation to be in the push button.

I guess I am stuck on how to pass the number from the edit box to the push button to convert it, then how to pass it back, then display it. Does this make any sense?

function varargout = ICA09A_TEMPFtoC(varargin)
% ICA09A_TEMPFTOC MATLAB code for ICA09A_TEMPFtoC.fig
%      ICA09A_TEMPFTOC, by itself, creates a new ICA09A_TEMPFTOC or raises the existing
%      singleton*.
%
%      H = ICA09A_TEMPFTOC returns the handle to a new ICA09A_TEMPFTOC or the handle to
%      the existing singleton*.
%
%      ICA09A_TEMPFTOC('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ICA09A_TEMPFTOC.M with the given input arguments.
%
%      ICA09A_TEMPFTOC('Property','Value',...) creates a new ICA09A_TEMPFTOC or raises     the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ICA09A_TEMPFtoC_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ICA09A_TEMPFtoC_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ICA09A_TEMPFtoC

% Last Modified by GUIDE v2.5 20-Mar-2013 13:14:08

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ICA09A_TEMPFtoC_OpeningFcn, ...
                   'gui_OutputFcn',  @ICA09A_TEMPFtoC_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
     gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before ICA09A_TEMPFtoC is made visible.
function ICA09A_TEMPFtoC_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ICA09A_TEMPFtoC (see VARARGIN)

% Choose default command line output for ICA09A_TEMPFtoC
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ICA09A_TEMPFtoC wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = ICA09A_TEMPFtoC_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in convert_pb.
function convert_pb_Callback(hObject, eventdata, handles)
% hObject    handle to convert_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
InputString = get(handles.convert_pb,'Convert');

InputNumber = str2num(InputString);

Result = (5 / 9) * (InputNumber - 32);

set(handles.result, 'Convert', Result);


function degF_et_Callback(hObject, eventdata, handles)
% hObject    handle to degF_et (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of degF_et as text
%        str2double(get(hObject,'String')) returns contents of degF_et as a double
UserInput = str2double(get(hObject,'String'))

% --- Executes during object creation, after setting all properties.
function degF_et_CreateFcn(hObject, eventdata, handles)
% hObject    handle to degF_et (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'),          get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

Is there anything I should do to make this more readable for anyone editing?

Upvotes: 0

Views: 6420

Answers (1)

Molly
Molly

Reputation: 13610

To get the temperature entered in the text edit box use:

tempF = get(handles.degF_et,'String');

This can be called from the push button function.

To change the string that is displayed there use:

set(handles.degF_et,'String','some string');

Upvotes: 1

Related Questions