MATLAB的GUI可以实现人机交互。在命令窗口中输入guide即可打开GUI工作界面。我们可以方便地建立控件以及编写程序,实现人机交互。​

下以摄氏度与华氏度的转换为例进行说明。​

最终效果如下图:​

总共有5个控件:2个静态文本(static text),2个可编辑文本(edit

text),1个滑动条(slider)。​

用户可以通过2个可编辑文本输入数值,也可以直接拖动滑动条实现取值。​因此总共有3个回叫函数callback。

程序代码:​​​

function varargout = temp_conver(varargin)

% TEMP_CONVER M-file for temp_conver.fig

%      TEMP_CONVER, by itself, creates a new TEMP_CONVER or raises the existing

%      singleton*.

%

%      H = TEMP_CONVER returns the handle to a new TEMP_CONVER or the handle to

%      the existing singleton*.

%

%      TEMP_CONVER('CALLBACK',hObject,eventData,handles,...) calls the local

%      function named CALLBACK in TEMP_CONVER.M with the given input arguments.

%

%      TEMP_CONVER('Property','Value',...) creates a new TEMP_CONVER or raises the

%      existing singleton*.  Starting from the left, property value pairs are

%      applied to the GUI before temp_conver_OpeningFunction gets called.  An

%      unrecognized property name or invalid value makes property application

%      stop.  All inputs are passed to temp_conver_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

% Copyright 2002-2003 The MathWorks, Inc.

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

% Last Modified by GUIDE v2.5 04-Mar-2016 17:07:22

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;

gui_State = struct('gui_Name',       mfilename, ...

'gui_Singleton',  gui_Singleton, ...

'gui_OpeningFcn', @temp_conver_OpeningFcn, ...

'gui_OutputFcn',  @temp_conver_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 temp_conver is made visible.

function temp_conver_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 temp_conver (see VARARGIN)

% Choose default command line output for temp_conver

handles.output = hObject;

% Update handles structure

guidata(hObject, handles);

% UIWAIT makes temp_conver wait for user response (see UIRESUME)

% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.

function varargout = temp_conver_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;

function edit1_Callback(hObject, eventdata, handles)

% hObject    handle to edit1 (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 edit1 as text

%        str2double(get(hObject,'String')) returns contents of edit1 as a double

deg_f=str2num(get(hObject,'string'));

deg_c=to_c(deg_f);

set(handles.edit1,'string',sprintf('%.1f',deg_f));

set(handles.edit2,'string',sprintf('%.1f',deg_c));

set(handles.slider1(2),'Value',deg_c);%下标2非常关键,是BUG,handles.slider输出有2个值,后面的值才是句柄

% --- Executes during object creation, after setting all properties.

function edit1_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit1 (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

set(hObject,'BackgroundColor','white');

else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));

end

function edit2_Callback(hObject, eventdata, handles)

% hObject    handle to edit2 (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 edit2 as text

%        str2double(get(hObject,'String')) returns contents of edit2 as a double

deg_c=str2num(get(hObject,'string'));

deg_f=to_f(deg_c);

set(handles.edit1,'string',sprintf('%.1f',deg_f));

set(handles.edit2,'string',sprintf('%.1f',deg_c));

set(handles.slider1(2),'Value',deg_c);

% --- Executes during object creation, after setting all properties.

function edit2_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit2 (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

set(hObject,'BackgroundColor','white');

else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));

end

% --- Executes on slider movement.

function slider1_Callback(hObject, eventdata, handles)

% hObject    handle to slider1 (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,'Value') returns position of slider

%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

deg_c=get(hObject,'value');

deg_f=to_f(deg_c);

set(handles.edit1,'string',sprintf('%.1f',deg_f));

set(handles.edit2,'string',sprintf('%.1f',deg_c));

set(handles.slider1(2),'Value',deg_c);

% --- Executes during object creation, after setting all properties.

function slider1_CreateFcn(hObject, eventdata, handles)

% hObject    handle to slider1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change

%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.

usewhitebg = 1;

if usewhitebg

set(hObject,'BackgroundColor',[.9 .9 .9]);

else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));

end

function deg_f=to_f(deg_c)

deg_f=(9/5)*deg_c+32;

function deg_c=to_c(deg_f)

deg_c=(5/9)*(deg_f-32);

matlab把华氏度,MATLAB GUI实例1——摄氏度与华氏度的转换相关推荐

  1. matlab学习——摄氏度与华氏度的转换

    代码如下 摄氏度转华氏摄氏 function C=Untitled() while 1a=('tempreature in C is: ');C = input(a);if isempty(C)ret ...

  2. 快速谱峭度matlab,一种基于快速谱峭度分析的泵潜在空化故障检测方法与流程

    本发明属于信号处理领域,尤其涉及一种基于快速谱峭度分析泵的实时状态并且检测其潜在空化故障的方法. 背景技术: 高性能离心泵在当今社会上广泛应用和需求巨大.由于工作在高压高速等复杂条件下,离心泵的空化故 ...

  3. 利用MATLAB模糊控制器实现对水位高度调节SIMULINK仿真(隶属度7分级)

    A.理论分析与设计 (1)确定模糊控制器的输入.输出变量 模糊控制器的两个输入变量,分别选为液位偏差E(设定液位高度r-实测液位高度y)和液位偏差变化率EC,输出模糊变量为控制阀门开度U: (2)确定 ...

  4. 2021-05-11 Matlab遗传算法工具箱的使用及实例(非线性规划)

    Matlab遗传算法工具箱的使用及实例(非线性规划) 本文将介绍MATLAB遗传算法工具箱求解非线性规划问题.在阅读本文之前,建议读者阅读上一期"MATLAB遗传算法工具箱求解线性规划问题& ...

  5. 基于MATLAB的数字图像处理系统GUI界面设计

    基于MATLAB的数字图像处理系统GUI界面设计 图像读入 从图形文件中读入图像 imread Syntax: A = imread(filename, fmt) filename:指定的灰度或彩色图 ...

  6. matlab最优控制实验报告_第十二篇 章 用MATLAB解最优控制问题及应用实例 最优控制课件.ppt...

    第十二篇 章 用MATLAB解最优控制问题及应用实例 最优控制课件.ppt 综上所述可得结论:Q=diag(1,0,0),R=2时,系统各方面响应较好. 矩阵Q变大时,反馈矩阵变大: 当Q的对角线上第 ...

  7. 用matlab做音乐仿真,Matlab课程设计报告--MATLAB GUI的音乐键盘仿真

    Matlab课程设计报告--MATLAB GUI的音乐键盘仿真 1 MATLAB MATLAB 实践实践 课程设计课程设计 目目 录录 1.1.设计目的设计目的3 3 2.2.题目分析题目分析3 3 ...

  8. Matlab plotyy画双纵坐标图实例

    转载自:http://blog.sina.com.cn/s/blog_49d955150100lxoe.html Matlab plotyy画双纵坐标图实例 x = 0:0.01:20; y1 = 2 ...

  9. 基于Matlab人脸识别签到系统(GUI界面)

    文件大小:5.3M 代码行数:298行(主程序) 开发环境:Matlab2016.2017.2018.2020.2021 点击下载:点击下载 简要概述:基于Matlab人脸识别签到系统(GUI界面) ...

最新文章

  1. secureCrt开启oracle
  2. nginx openresty content_by_lua_file 404错误
  3. 如何使用Jekyll+GitHub Pages搭建个人博客站点
  4. SQL Server Window Function 窗体函数读书笔记二 - A Detailed Look at Window Functions
  5. 【bzoj1486】【[HNOI2009]梦幻布丁】启发式链表合并(详解)
  6. 12款优秀jQuery Ajax分页插件和教程
  7. Hadoop生态圈介绍
  8. 吃货在东京 -- 记那段吃不饱的日子 之一 牛头自助烤肉店
  9. UvaLive 4670 Dominating Patterns
  10. 使用Django+MySQL快速搭建一个属于自己的网站
  11. Android给图片加水印
  12. 用 LaTeX 写漂亮学位论文(from wloo)
  13. AUC值越大_AUC,ROC 讲解
  14. 【c++ -- 谓词】
  15. 如何解决英文版Windows10下中文显示乱码问题?
  16. C语言新手常犯的错误-截断
  17. STM32 无刷电机BLDC 1KW带刹开发板 PDF原理图 源代码 MDK源码
  18. excel中如何依据日期相等实现数据匹配
  19. java lotus_Java开发网 - 访问lotus notes
  20. android边直播边录制视频软件,实现Android本机 视频录制播放 边录边放

热门文章

  1. 清华大学计算机考研机试KY6 手机键盘
  2. 恢复通讯录显示服务器开小差,恢复备份数据通讯录还是没有找到数据怎么办?...
  3. Scratch的方向
  4. 对计算机社团未来发展的看法,对社团发展的一些看法
  5. matlab自动写word报告,matlab自动写入word
  6. 使用蓝牙连接设备显示无法连接的解决方案
  7. 二级管的正向恢复与反向恢复时间
  8. 周鸿祎自称3次破解特斯拉云端系统:安全隐患巨大
  9. html 下拉图片列表,图片、表单、下拉选项
  10. 周鸿祎:互联网成功十大案例