Matlab GUI,使用静态文本框的输入并使用基本按钮进行转换

时间:2013-03-20 18:36:16

标签: matlab user-interface

我正在努力解决Matlab gui界面的工作原理。

我不是在寻找答案,只是对如何做到这一点的更多指导。

我正在尝试将编辑框中的温度从F转换为C ...所以我认为我需要将方程式放在按钮中。

我想我仍然坚持如何将数字从编辑框传递到按钮进行转换,然后如何传回,然后显示它。 这有什么意义吗?

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

我是否应该做些什么来让任何编辑人员都更具可读性?

1 个答案:

答案 0 :(得分:1)

要在文本编辑框中输入温度,请使用:

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

这可以通过按钮功能调用。

要更改显示的字符串,请使用:

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