背景:学习matlab gui编程入门,完成一个基于GUIDE的图形化界面程序,结合信号生成及分析等。
操作步骤:
1、新建程序
新建一个GUIDE程序
这里选择第一个选项,即创建一个空白的GUIDE模板(下面的三个选项为matlab自带的3个guide模板,可以尝试使用,但是空白模板更灵活一些)。
创建完成后,将会得到这样一个面板,这就是进行matlab进行guide图形化编程界面,在这里我们可以添加我们需要的各种控件到面板中。
可以看到上图中的,左侧有7行2列共14个常用的控件,添加时直接拖动即可,右侧的带油栅格的面板就是完成guide变成后,程序运行时的图形化界面。
7行2列控件分别为:
普通按钮 | 滑动条 |
单选按钮 | 复选按钮 |
可编辑文本 | 静态文本 |
弹出式菜单 | 列表框 |
切换按钮 | 表 |
坐标轴 | 面板 |
按钮组 | ActiveX控件 |
其中常用的控件包括:普通按钮、滑动条、可编辑文本、静态文本、坐标轴等。
2、图形化界面编程
首先可以根据预计生成的面板的大小,拉伸编程面板。
然后将需要的控件拖拽到编程面板中,并进行布局。这里可以使用上方工具栏的“对其对象”工具,进行布局。如:
完成布局后可以双击各个控件,修改其属性,控件的属性面板如下:(以滑动条为例)
下面,对几个比较重要的属性进行介绍:
字体属性:
滑动条变量取值范围:
标注文字(String)&标签(Tag):标签是最重要的属性,在代码中对每一个控件进行操作时,都是以其tag作为索引。
单位(Units)&初始值(Value):单位一般会选择charaters(绝对单位)和normalized(相对单位),选择characters则输出值为设定的最大最小值之间的实际值,选择normalized则输出值为归一化后的0~1之间的值。
其他各个控件的属性会有一些区别,和各个控件自身的特性有关,基本可以直接根据各个属性的字面意思理解。
完成控件的添加、布局和属性设定后,即可得到一个如下的界面图:
3、m文件编程:
完成上述图形编程后,点击保存,则会自动根据我们的设定,生成一个m文件模板。
模板中主要有以下几个部分:
1、程序初始化
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct(\'gui_Name\', mfilename, ...
\'gui_Singleton\', gui_Singleton, ...
\'gui_OpeningFcn\', @signal_generator_OpeningFcn, ...
\'gui_OutputFcn\', @signal_generator_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
注意,这部分自动生成的初始化代码,一般不要修改!
2、程序open founction
% --- Executes just before signal_generator is made visible.
function signal_generator_OpeningFcn(hObject, eventdata, handles, varargin)
这部分代码会在完成图形界面控件的创建后,首先运行,一般可以在这里添加一些自定义变量的初始化代码等。
3、控件回调函数 callback
% --- Executes on button press in noise.
function noise_Callback(hObject, eventdata, handles)
这部分函数会分别对应于我们在图形编程界面添加的每一个控件,当某一个控件发生操作时,程序对自动调用它对应的回调函数(类似于中断回调函数)。
4、我们在生成的m文件中,自行添加自定义函数,完成我们需要执行的任务。将自定义函数在控件的回调函数中国调用即可。
以下为我完成的信号发生+时域分析 GUI程序的m文件代码,仅供参考:(文件可以在我的csdn中下载:https://download.csdn.net/download/qq_40464014/12265321)
function varargout = signal_generator(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct(\'gui_Name\', mfilename, ...
\'gui_Singleton\', gui_Singleton, ...
\'gui_OpeningFcn\', @signal_generator_OpeningFcn, ...
\'gui_OutputFcn\', @signal_generator_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 signal_generator is made visible.
function signal_generator_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
global t;
global run;
% text windos init
set(handles.fre_text,\'string\',get(handles.fre_slider,\'value\'));
set(handles.amp_text,\'string\',get(handles.amp_slider,\'value\'));
% x axes data
%handles.x = linspace(0,0.03,441*3);
handles.x = linspace(0,1.5,44100*1.5);
% wave shape data
handles.shape = 2;
% data init
t = 0;
run = 0;
handles
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = signal_generator_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function my_callback(handles)
global sound_y;
global plot_y;
global plot_x;
global t;
x = handles.x;
fre = get(handles.fre_slider,\'value\');
amp = get(handles.amp_slider,\'value\');
% white noise
if handles.shape == 1
sound_y = 0.5*amp*wgn(1,44100*1.5,1);
% sin singal
elseif handles.shape == 2
sound_y = amp*sin(2*pi*fre*x);
% square singal
elseif handles.shape == 3
sound_y = amp*square(2*pi*fre*x);
% trangle singal
elseif handles.shape == 4
sound_y = amp*sawtooth(2*pi*fre*x,0.5);
end
plot_x = x(:,(1+t):(3*441+t));
plot_y = sound_y(:,(1+t):(3*441+t));
plot(handles.monitor,plot_x,plot_y);
set(handles.monitor,\'ylimmode\',\'manual\');
set(handles.monitor,\'ylim\',[-2000,2000]);
% get peak value
peak_max = max(plot_y);
peak_min = min(plot_y);
peak_value = peak_max - peak_min;
set(handles.peak,\'string\',num2str(peak_value));
% get mean value
mean_value = mean(plot_y);
set(handles.mean,\'string\',num2str(mean_value));
% get rms value
rms_value = rms(plot_y);
set(handles.rms,\'string\',num2str(rms_value));
% % get frequency value
% fre_value = fft(plot_y);
% aaa = abs(fre_value);
% [m,p] = max(aaa);
% set(handles.std,\'string\',num2str(p));
% get std error value
std_value = std(plot_y);
set(handles.std,\'string\',num2str(std_value));
% --- Executes on button press in noise.
function noise_Callback(hObject, eventdata, handles)
handles.shape = 1;
guidata(hObject, handles);
my_callback(handles);
% --- Executes on button press in square.
function square_Callback(hObject, eventdata, handles)
handles.shape = 3;
guidata(hObject, handles);
my_callback(handles);
% --- Executes on button press in trangle.
function trangle_Callback(hObject, eventdata, handles)
handles.shape = 4;
guidata(hObject, handles);
my_callback(handles);
% --- Executes on button press in sin.
function sin_Callback(hObject, eventdata, handles)
handles.shape = 2;
guidata(hObject, handles);
my_callback(handles);
% --- Executes on slider movement.
function fre_slider_Callback(hObject, eventdata, handles)
set(handles.fre_text,\'string\',get(handles.fre_slider,\'value\'));
my_callback(handles);
% --- Executes during object creation, after setting all properties.
function fre_slider_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,\'BackgroundColor\'), get(0,\'defaultUicontrolBackgroundColor\'))
set(hObject,\'BackgroundColor\',[.9 .9 .9]);
end
function fre_text_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function fre_text_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,\'BackgroundColor\'), get(0,\'defaultUicontrolBackgroundColor\'))
set(hObject,\'BackgroundColor\',\'white\');
end
% --- Executes on slider movement.
function amp_slider_Callback(hObject, eventdata, handles)
set(handles.amp_text,\'string\',get(handles.amp_slider,\'value\'));
my_callback(handles);
% --- Executes during object creation, after setting all properties.
function amp_slider_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,\'BackgroundColor\'), get(0,\'defaultUicontrolBackgroundColor\'))
set(hObject,\'BackgroundColor\',[.9 .9 .9]);
end
function amp_text_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function amp_text_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,\'BackgroundColor\'), get(0,\'defaultUicontrolBackgroundColor\'))
set(hObject,\'BackgroundColor\',\'white\');
end
% --- Executes on button press in run.
function run_Callback(hObject, eventdata, handles)
global t;
global run;
run = 1;
while run == 1
t = t+2;
my_callback(handles);
set(handles.monitor,\'xlimmode\',\'manual\');
set(handles.monitor,\'xlim\',[(1+t)*0.000022,(3*441+t)*0.000022]);
pause(0.01);
end
% --- Executes on button press in stop.
function stop_Callback(hObject, eventdata, handles)
global run;
run = 0;
% --- Executes on button press in play.
function play_Callback(hObject, eventdata, handles)
global sound_y;
sound(sound_y,44100);
% --- Executes on button press in clear.
function clear_Callback(hObject, eventdata, handles)
delete(allchild(handles.monitor));
function peak_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function peak_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,\'BackgroundColor\'), get(0,\'defaultUicontrolBackgroundColor\'))
set(hObject,\'BackgroundColor\',\'white\');
end
function mean_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function mean_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,\'BackgroundColor\'), get(0,\'defaultUicontrolBackgroundColor\'))
set(hObject,\'BackgroundColor\',\'white\');
end
function rms_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function rms_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,\'BackgroundColor\'), get(0,\'defaultUicontrolBackgroundColor\'))
set(hObject,\'BackgroundColor\',\'white\');
end
function std_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function std_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,\'BackgroundColor\'), get(0,\'defaultUicontrolBackgroundColor\'))
set(hObject,\'BackgroundColor\',\'white\');
end
——cloud over sky
——2020/3/23
请发表评论