我所使用的MATLAB版本——MATLAB R2017a

MATLAB的GUI的操作其他人写的很清楚了,在此不再赘述。

MATLAB的GUI的基本操作可见:GUI基本操作

这次所设计的钢琴有简单的七个琴键,DO RE MI FA SO LA XI

首先在命令行中输入

guide

即可调出gui设计页面,选择第一个选项的空白GUI,然后点确定即可

在工具栏的右侧选择按钮,在界面上画出一长方形代表按键,并用ctrl+c,ctrl+v创造出七个琴键,并对齐排列

双击按键可打开具体的参数设置,其中有前景色、背景色、字体、标志、类型等,可以更加的个性化。

其中的STRING代表在GUI界面的名字,tag是生成的代码中的标志(在保存后还会生成一代码文件)

修改后即可看到第一个琴键的名字为DO,同理我们可以把其余其他按键命名,最后我们可以得到如下图所示:

在保存之后会自动生成一代码,其中大部分我们不需要关心,我们要关注的就是其中的Do_Callback()等回调函数(Do是我们命名的tag使然),也就是我们按一次按键会执行一次回调函数。

function Do_Callback(hObject, eventdata, handles)

% hObject handle to Do (see GCBO)

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

% handles structure with handles and user data (see GUIDATA)

为了发出钢琴声我们还需要编写一发声函数,具体代码如下:

function y = gen_wave( tone, rythm )

% 音调 拍

Fs = 8192;

freqs = [523, 587, 659, 698, 783, 880, 988];

x = linspace(0, 2 * pi * rythm, floor(Fs * rythm));

y = sin(freqs(tone) * x) .*(1- x/(rythm * 2 *pi));

end

函数具体内容参见我的另一博客:MATLAB如何创造音乐

所以在DO按键的回调函数中应该这样写:

function Do_Callback(hObject, eventdata, handles)

% hObject handle to Do (see GCBO)

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

% handles structure with handles and user data (see GUIDATA)

y = gen_wave(1,1);

sound(y,8192);

同理在此给出所有代码:

function varargout = piano(varargin)

% PIANO MATLAB code for piano.fig

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

% singleton*.

%

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

% the existing singleton*.

%

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

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

%

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

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

% applied to the GUI before piano_OpeningFcn gets called. An

% unrecognized property name or invalid value makes property application

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

% Last Modified by GUIDE v2.5 06-Feb-2020 16:54:20

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;

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

'gui_Singleton', gui_Singleton, ...

'gui_OpeningFcn', @piano_OpeningFcn, ...

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

function piano_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 piano (see VARARGIN)

% Choose default command line output for piano

handles.output = hObject;

% Update handles structure

guidata(hObject, handles);

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

% uiwait(handles.figure1);

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

function varargout = piano_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 Do.

function Do_Callback(hObject, eventdata, handles)

% hObject handle to Do (see GCBO)

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

% handles structure with handles and user data (see GUIDATA)

y = gen_wave(1,1);

sound(y,8192);

% --- Executes on button press in Re.

function Re_Callback(hObject, eventdata, handles)

% hObject handle to Re (see GCBO)

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

% handles structure with handles and user data (see GUIDATA)

y = gen_wave(2,1);

sound(y,8192);

% --- Executes on button press in Mi.

function Mi_Callback(hObject, eventdata, handles)

% hObject handle to Mi (see GCBO)

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

% handles structure with handles and user data (see GUIDATA)

y = gen_wave(3,1);

sound(y,8192);

% --- Executes on button press in Fa.

function Fa_Callback(hObject, eventdata, handles)

% hObject handle to Fa (see GCBO)

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

% handles structure with handles and user data (see GUIDATA)

y = gen_wave(4,1);

sound(y,8192);

% --- Executes on button press in So.

function So_Callback(hObject, eventdata, handles)

% hObject handle to So (see GCBO)

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

% handles structure with handles and user data (see GUIDATA)

y = gen_wave(5,1);

sound(y,8192);

% --- Executes on button press in La.

function La_Callback(hObject, eventdata, handles)

% hObject handle to La (see GCBO)

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

% handles structure with handles and user data (see GUIDATA)

y = gen_wave(6,1);

sound(y,8192);

% --- Executes on button press in Xi.

function Xi_Callback(hObject, eventdata, handles)

% hObject handle to Xi (see GCBO)

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

% handles structure with handles and user data (see GUIDATA)

y = gen_wave(7,1);

sound(y,8192);

再结合我们的 gen_wave()函数即可发出音乐声(有时候刚开始可能会有一些延迟。。。)

以上就是用MATLAB的GUI如何设计钢琴界面并能发声的教程了,如果感觉有意思,点个赞,O(∩_∩)O哈哈~

参考博客:GUI基本知识

gen_wave函数设计

matlab gui 钢琴,基于MATLAB中的GUI设计的钢琴界面设计并能发声相关推荐

  1. 基于matlab的gui图像处理,基于matlab GUI的数字图像处理系统毕业论文+开题报告+程序+设计文献...

    摘  要 数字图像处理(Digital Image Processing)又称为计算机图像处理,它是指将图像信号转换成数字信号并利用计算机对其进行处理的过程.在数字图像处理过程中,输入的是质量低的图像 ...

  2. 「电子万年历matlab仿真」——基于Matlab的电子万年历仿真实现

    「电子万年历matlab仿真」--基于Matlab的电子万年历仿真实现 作为一种具有时间显示.日期查询.闹钟提醒等功能的电子产品,电子万年历已经成为了人们日常生活中不可或缺的一部分.而在现代科技的发展 ...

  3. matlab与钻井平台,基于MATLAB环境下采用C8051F060实现钻井井眼姿态监测系统的设计...

    使用串口进行读写操作时,还需注意的一点就是对串口数据校验方式属性(Parity)的设置,如果该设置与下位机软件不匹配,将造成读写错误而又难以查找原因. 5.2 GUI界面中activx控件的使用 在M ...

  4. 怎么做极简ui设计?UI界面设计中的极简原则【萧蕊冰】

    怎么做极简ui设计?近几年,互联网的飞速发展引起了科技领域的巨大浪潮,UI设计行业需求大量专业优秀人才,吸引了很多想要学习UI设计的人.UI设计也就是用户界面的设计,作为引导用户快速正确使用产品的主要 ...

  5. matlab gui 图像增强,基于MATLAB GUI的图像增强技术的实现

    基于MATLAB GUI的图像增强技术的实现 英英 [期刊名称]<内蒙古广播与电视技术> [年(卷),期]2017(034)003 [摘要]随着数字化时代的到来,人们对图像处理的要求越来越 ...

  6. 【CV/Matlab系列】基于matlab GUI的视频监控界面

    DATE: 2020.10.18 updated at 2022.04.23: 界面上增加报警提示功能 文章目录 1.前言 2.视频监控界面 2.1.初始化界面 2.2.人脸检测和计数功能 2.3.监 ...

  7. matlab伺服驱动,基于MATLAB的GUI设计伺服驱动系统仿真软件

    0引言计算机仿真技术是设计.研究和开发现代机械电子设备的新型手段,具有巨大的优越性,目前我们在机床伺服系统的工程设计中已广泛采用了这项技术.当前较流行的是采用MATLAB软件包进行设计,该软件包对控制 ...

  8. matlab gui电机,基于MATLAB GUI的感应电机性能分析界面设计.docx

    摘要:本设计通过键入GUIDE命令打开GUI编辑界面,GUI(Graphical User Interfaces)意指图形用户界面,相较于命令行界面更容易在视觉上被接受. 用户可通过一定方法,如键盘鼠 ...

  9. matlab 函数return_基于MATLAB的指纹识别系统【论文,GUI】

    一.课题介绍 本设计为基于MATLAB的指纹识别系统.本设计系统主要对指纹图像进行三方面处理:图像预处理.特征提取和特征匹配.图像预处理包括四个步骤:图像灰度化.滤波增强.二值化.细化,对指纹图像进行 ...

  10. 如何用matlab画旋转面,基于MATLAB在旋转面及其方程教学中应用的教学设计

    汝强 [摘 要]在空间解析几何教学中,MATLAB不仅能将复杂的空间曲面方程精准.直观地用三维图形表现出来,还能以动画的形式将空间复杂曲面任意旋转,使学生能直观地.全方位地观察.理解空间曲面方程所表示 ...

最新文章

  1. 2021年就业力排名TOP10的英国大学
  2. php管理员登录文件,使用PHP文件重置管理员密码(Drupal 8)
  3. JQuery的$和其它JS发生冲突的快速解决方法
  4. 嵌入式驱动自学者的亲身感受,有什么建议?
  5. linux中mysql与eclipse_Linux下eclipse CDT及mysql安装,c++访问mysql数据库
  6. 专用计算机 一级专用计算机芯片,计算机一级考试 2015年模拟题(一)
  7. 一个老鸟眼中“IT民工”的发展方向
  8. 个人项目需求与分析——点菜系统App
  9. Xcode查看CoreData的SQL语句
  10. ReactiveCocoa之UI篇
  11. 求1+2阶乘+3阶乘+ 省略 +20阶乘的和
  12. 三大国产操作系统,到底哪个最好用
  13. java如何调用百度地图拾取坐标系统
  14. 【推荐】智慧医疗应用和研究资料合集
  15. [游戏程序] 经典游戏服务器端架构概述
  16. Vue中使用 Aplayer 和 Metingjs 添加音乐插件
  17. Windows10使用浏览器崩溃复现及分析
  18. Java面向可复用性和可维护性的设计模式
  19. 基于深度学习的目标检测算法综述
  20. HIT计算机系统大作业-程序人生-Hello’s P2P

热门文章

  1. ubuntu 14.04 root破解
  2. dede使用方法---用js让当前导航高亮显示
  3. 链表的实现(Java语言描述)
  4. TableView数据源方法的执行顺序
  5. Object-c 学习笔记
  6. linux下查看表类型注释命令@tcc
  7. 数据库表之间的数据导入 sql语句
  8. 系统目录WINDOWS下主要文件夹简介
  9. HttpUrlConnection的简单使用--get和post的简单使用
  10. cookie里面用到的关键字_Java的理解角度-关键字篇