目录
一、Matlab知多少
二、Matlab 类的使用
三、Matlab GUIDE使用
四、Matlab机器学习
五、Matlab Coder
六、 MException异常类
七、APP Designer
%符号变量
>> syms x y z
>> f=(x+y)^2
>> expand(f)
ans = x^2 + 2*x*y + y^2
>>simplify(ans)
ans =(x + y)^2
>>subs(f,x,z) %把x换成z
>> isa(ans,'double')
ans =logical 0
%匿名函数
>>f=@(x) 2*x
>>f=@(x,y) x^2+y^2
>>f1=@(x)f(x,1)
>>[x,val]=fminsearch(f1,0.5)
>>bsxfun(@add,a,b)
%矩阵集合操作
>>union %AUB
>>intersect %A∩B
>>sectxor %AUB-A∩B
>>setdiff %A-AB
%table
>> age=[1;2;3];
>> weight=[30;25;22];
>> name={'A';'B';'C'};
>> f=table(age,weight,'RowNames',name)
>> f({'A'},:)
ezplot
strrep查找字符并替换
chr = 'The quick brown fox' newChr = strrep(chr,'quick','sly') newChr = 'The sly brown fox'
strfind 在字符矢量中查找多次出现的模式的起始索引,区分大小写
str = 'Find the starting indices of a pattern in a character vector';
k = strfind(str,'in')
k = 2 15 19 40
strsplit在指定的分隔符处拆分字符串
在空白处将 C
= strsplit(str
)str
拆分为 C
。空白字符等效于集合 {' ','\f','\n','\r','\t','\v'}
中的任何转义序列。
在 C
= strsplit(str
,delimiter
)delimiter
指定的分隔符处拆分 str
。
使用一个或多个名称-值对组参数指定其他分隔符选项。C
= strsplit(str
,delimiter
,Name,Value
)
[
还返回数组 C
,matches
] = strsplit(___)matches
。matches
输出参数包含 strsplit
拆分 str
时遇到的所有分隔符。您可以将此语法与前面语法中的任何输入参数结合使用
全局变量 global
function globalDemo
%static int x = 1;
%mlock:
global x;%declare
if isempty(x)
x=1;%define
else
x = x+1;
end
end
全局变量persistent :只能在a.m文件下访问,不可以跨文件访问
如果不希望全局变量被clear清空,使用mlock
2.1 matlab 基类
Matlab 可以使用面对对象的思路编程,在新建中选择新建类即可。新建类有两个基类value类(默认),handle类,却别在于选择继承handle类,类的属性可以在调用方法改变属性的时候同步改变。另外类的脚本中obj类似this或者self指的是自己这个类,它可以访问自己的属性。
classdef MAN
%UNTITLED 此处显示有关此类的摘要
% 此处显示详细说明
properties
age
height
end
methods
function obj = MAN(inputArg1,inputArg2)
%UNTITLED 构造此类的实例
% 此处显示详细说明
obj.age = inputArg1;
obj.height = inputArg2;
end
function obj = grow(obj,inputArg)
%METHOD1 此处显示有关此方法的摘要
% 此处显示详细说明
obj.age = obj.age + inputArg;
end
end
end
>> a=MAN(5,10);
>> a.grow(5)
ans =
10
>> a
a =
MAN - 属性:
age: 5
height: 10
类继承handle类
classdef MAN > b=MAN(5,10);
>> b.grow(5)
ans =
MAN - 属性:
age: 10
height: 10
>> b
b =
MAN - 属性:
age: 10
height: 10
2.2 matlab继承
类的属性默认是公有的,私有属性设置为properties(Access=private),同理methods也可以设置私有和公有方法。
classdef MAN
properties(Access=private)%properties(SetAccess=private)
age
end
properties
height
end
end
类的继承:
classdef Student < MAN
properties
grade
end
methods
function obj = Student(age,height,grade)
obj = obj@MAN(age,height);%构造父类
obj.grade = grade;
end
function progress(obj,num)
obj.grade = obj.grade + num;
end
end
end
2.3 运算符重载
matlab中每个运算符如+-等都有对应的函数,如+ = plus
classdef Student < MAN
properties(SetAccess=private)
grade
end
methods
function obj = Student(age,height,grade)
obj = obj@MAN(age,height);%构造父类
obj.grade = grade;
end
function progress(obj,num)
obj.grade = obj.grade + num;
end
function plus(obj1,obj2)
%限制类型(对象,类型,限制)
%validateattributes(obj2,{'Student'},{'nonempty'});
obj1.grade=obj1.grade+obj2.grade;
end
end
end
>> a= Student(10,150,60)
>> b= Student(10,160,80)
>> a+b
>> a
a =
Student - 属性:
grade: 140
height: 150
3.1 GUIDE基本使用
GUIDE作为Matlab的UI制作界面简洁而朴素,不失为建立简单ui的一个选择。选择新建 APP 中的GUIDE,我们可以在属性的Tag中设置控件的名字。
按钮会自动生成一个回调函数,该回调函数的名字根据Tag命名的。
% --- Executes on button press in myButton.
function myButton_Callback(hObject, eventdata, handles)
% hObject handle to myButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
disp([hObject.String]);
set(handles.mybtnText,'String','world');
第一个参数hObject表示按钮对象本身,第二个参数eventdata表示事件触发
第三个参数handles表示整个图形界面的句柄,主要是图形控件
点击ui的运行按钮,然后点击button
传递数据可以在handles结构体中新定义一个变量,然后在回调函数中使用。使用guidata(gcf句柄,存储变量)可以实现数据的保存(gcf表示get current figure)。
% --- Executes just before demoGui is made visible.
function demoGui_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 demoGui (see VARARGIN)
% Choose default command line output for demoGui
handles.output = hObject;
handles.count = 0;
% Update handles structure
guidata(hObject, handles);
guidata(gcf,handles);
% UIWAIT makes demoGui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
3.2 GUIDE画个坐标
新建一个draw的guide,然后根据选择的函数,点击按钮绘制曲线
设置坐标系的属性XY轴初始长度
% --- Executes just before draw is made visible.
function draw_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 draw (see VARARGIN)
% Choose default command line output for draw
handles.output = hObject;
handles.axes1.YLim(1)=-1.5;
handles.axes1.YLim(2)=1.5;
handles.axes1.XLim(1)=-2*pi-1;
handles.axes1.XLim(2)=2*pi+1;
% Update handles structure
guidata(hObject, handles);
点击按钮的回调函数中设置:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str = handles.popupmenu1.String;
val = handles.popupmenu1.Value;
fcn = str(val);
disp(fcn)
%disp(handles.axes1.XLim);
%disp(handles.axes1.YLim);
dataX = handles.axes1.XLim(1):0.1:handles.axes1.XLim(2);
if strcmp(fcn,'sin(x)')
plot(dataX,sin(dataX));
elseif strcmp(fcn,'cos(x)')
plot(dataX,cos(dataX));
elseif strcmp(fcn,'tan(x)')
plot(dataX,tan(dataX));
end
3.3 GUIDE菜单栏
在工具栏中打开菜单编辑器,然后对菜单进行命名。
在菜单的回调函数中设置如下
function open_Callback(hObject, eventdata, handles)
% hObject handle to open (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
message('open');
draw();
% --------------------------------------------------------------------
function close_Callback(hObject, eventdata, handles)
% hObject handle to close (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
r = questdlg('关闭?','关闭请求','是','否','否');
if r == '是'
close all;
end
如果想更多了解,请查看‘帮助文档,更多示例’
分类
曲线拟合
自定义function 并打开Matlab Coder
function y= myinv(x)
%MYINV 此处显示有关此函数的摘要
% 此处显示详细说明
y = inv(x);
end
设置输入类型
命令行窗口输入:mex -setup
mex -setup C++
设置生成代码的语言
生成代码如下,可以在matlab和VS中相互验证:
返回最近引发的未捕获的 exception
= MException.lastMException
对象内容。如果最后一个异常是由 try/catch
语句捕获的,则不设置MException.last
。MException.last
是 MException
类的静态方法。
示例一: try - catch
try
a = notaFunction(5,6);
catch ME
if strcmp(ME.identifier,'MATLAB:UndefinedFunction')
warning('Function is undefined. Assigning a value of 0.');
else
rethrow(ME)
end
end
>> MExcept
警告: Function is undefined. Assigning a value of 0.
> In MExcept (line 5)
示例二: MException类
如果工作区中没有输入变量名称,则会引发异常。
str = input('Type a variable name: ','s');
if ~exist(str,'var')
ME = MException('MyComponent:noSuchVariable', ...
'Variable %s not found',str);
throw(ME)
end
>> MExcept
Type a variable name: tatal
错误使用 MExcept (line 6)
Variable tatal not found
示例三、直接创建ME
msgID = 'MATLAB:test';
msgtext = 'There are %d errors on this page';
A1 = 10;
ME = MException(msgID,msgtext,A1)
MException - 属性:
identifier: 'MATLAB:test'
message: 'There are 10 errors on this page'
cause: {}
stack: [0×1 struct]
然后开发人员可以查看调用类属性或者方法。
新建App Designer , 在视图左边,拖拽控件,右击右边控件可以添加回调函数,在代码视图控制app对象具体的成员即可
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
ret = questdlg('是否关闭窗口','窗口关闭','No');
if strcmp(ret,'Yes')
delete(app)
end
end