MATLAB中关于try...catch...end的用法

我们知道,matlab的代码是按行执行的,如果碰到错误行,则程序中断。try..catch可以使得可能出错代码不影响后面代码的继续执行,也可以检查,排查,解决程序的一些错误,增强代码的鲁棒性和可靠性。

1、try...end
try...end用于尝试运行一段也许可能出错的代码,比如:

m = rand(3,4); 
n = magic(5);
try 
        a = m*n; 
        disp(a)
end 
disp(m)


这段代码里面,a = m*n运行会出错,不满足矩阵乘法的原则。所以,a = m*n和disp(a)不执行,但后面的disp(m)亦然会执行

2、try...catch...end
m = rand(3,4);
n = magic(5);
try
        a = m*n;       %当程序碰到 a = m*n;错误后,就会跳转到catch里面的语句,继续执行,有点类似于if...else...end
        disp(a)
catch
        disp(size(m))
        disp(size(n))
end
disp(m)


2、try...catch...end用于检查错误
m = rand(3,4);
n = magic(5);
try
        a = m*n;
        disp(a)
catch err
        disp(size(m))
        disp(size(n))
end
disp(m)
  • 24
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
菜鸟提问基于MATLAB的防盗报警系统的有关问题-sounddetection.m 话不多说,之前有人做出视频防盗系统,详见此贴。 原来的一个版本:视频监控,有东西经过监控区域就报警: 贴子如下: https://www.ilovematlab.cn/thread-133835-1-1.html 后来又有人加上了声音报警系统。声音过大就会报警。 详见此贴 声音监控,有东西发出声音就进行报警 https://www.ilovematlab.cn/thread-133881-1-1.html 。。 然后就是我们有个大作业,我想到的是将两者结合在一起,在视频监控的同时,如果声音过大就进行报警,然后拍照(要是能把照片发到邮箱就好了)。 以下是我的修改,但是一直报错。希望有人能帮忙修改下实现上面的功能。 代码贴出: function intruderdetecting2 % 运动参数阈值 motionThreshold = 200;         %  0 ~ 255 secondsToRecord = 10; % 声音参数阈值 soundThreshold = 0.1;         % 0.0 ~ 1.0 %声音麦克风 ai = analoginput; addchannel; set; set; set; samplesToRecord = ceil; set; set; set; set; %启动摄像头 a = imaqhwinfo; [camera_name, camera_id, format] = getCameraInfo; vid = videoinput; set; set; triggerconfig; start; trigger; try     wait; catch     stop; end %启动函数,时间函数 set; set; set; %创建figure fig = figure('DoubleBuffer','on', ...     'Name', 'Intruder Detection', ...     'NumberTitle', 'off', ...     'WindowStyle', 'docked', ...     'Toolbar', 'none', ...     'MenuBar', 'none', ...     'Color',[.1 .1 .1], ...     'CloseRequestFcn', @figureCloseFcn, ...     'DeleteFcn', @figureDeleteFcn); %对界面初始化 timePrevious = []; start; imagePrevious = []; start; %视频的启动函数     function videoStartFcn             imagePrevious = getsnapshot;     end %声音启动函数     function soundStartFcn             timePrevious = now;     end %视频时间函数         function videoTimerFcn         try             imageCurrent = getsnapshot;             timeCurrent = now;         catch             % getsnapshot can fail if object is deleted while we are waiting.             return;         end samplesRequested = ceil * *... ai.SampleRate); warning; try sound = peekdata; catch sound = zeros); end warning; sound = sound - mean); % Center about the mean. soundMax = max)); % Calculate max deviation from mean. timePrevious = timeCurrent; % Make our figure current. figOld = get; %if fig ~= figOld %set; %end %plot;         imageDifference = abs;         imageMax = max);         imagePrevious = imageCurrent;         figOld = get;         if fig ~= figOld             set;         end         image;         label = datestr;         xlabel;         set;         if imageMax > motionThreshold             motion = true;         else             motion = false;         end         if ~islogging             % 如果有东西闯入,进行响应             if motion                 % 将figure变红                 set;                 % 将闯入者的视频发送到你的邮箱                 str=['SB.jpg'];                 imwrite; %保存拍下来的图像                 customIntruderAction();             else                 set;             end;         end     if ~islogging    % if noise       customIntruderAction();%有人闯入时发进行报警         if fig ~= figOld             set;         end    % end     end    %function customIntruderAction()      % [y,fs]=wavread;      % sound; end     function figureCloseFcn         %关闭视频和声音         try             stop;             stop;         catch         end         closereq;     end     function figureDeleteFcn         delete;         delete;     end end 纯M文件编程。
菜鸟提问基于MATLAB的防盗报警系统的有关问题-voice.m 话不多说,之前有人做出视频防盗系统,详见此贴。 原来的一个版本:视频监控,有东西经过监控区域就报警: 贴子如下: https://www.ilovematlab.cn/thread-133835-1-1.html 后来又有人加上了声音报警系统。声音过大就会报警。 详见此贴 声音监控,有东西发出声音就进行报警 https://www.ilovematlab.cn/thread-133881-1-1.html 。。 然后就是我们有个大作业,我想到的是将两者结合在一起,在视频监控的同时,如果声音过大就进行报警,然后拍照(要是能把照片发到邮箱就好了)。 以下是我的修改,但是一直报错。希望有人能帮忙修改下实现上面的功能。 代码贴出: function intruderdetecting2 % 运动参数阈值 motionThreshold = 200;         %  0 ~ 255 secondsToRecord = 10; % 声音参数阈值 soundThreshold = 0.1;         % 0.0 ~ 1.0 %声音麦克风 ai = analoginput; addchannel; set; set; set; samplesToRecord = ceil; set; set; set; set; %启动摄像头 a = imaqhwinfo; [camera_name, camera_id, format] = getCameraInfo; vid = videoinput; set; set; triggerconfig; start; trigger; try     wait; catch     stop; end %启动函数,时间函数 set; set; set; %创建figure fig = figure('DoubleBuffer','on', ...     'Name', 'Intruder Detection', ...     'NumberTitle', 'off', ...     'WindowStyle', 'docked', ...     'Toolbar', 'none', ...     'MenuBar', 'none', ...     'Color',[.1 .1 .1], ...     'CloseRequestFcn', @figureCloseFcn, ...     'DeleteFcn', @figureDeleteFcn); %对界面初始化 timePrevious = []; start; imagePrevious = []; start; %视频的启动函数     function videoStartFcn             imagePrevious = getsnapshot;     end %声音启动函数     function soundStartFcn             timePrevious = now;     end %视频时间函数         function videoTimerFcn         try             imageCurrent = getsnapshot;             timeCurrent = now;         catch             % getsnapshot can fail if object is deleted while we are waiting.             return;         end samplesRequested = ceil * *... ai.SampleRate); warning; try sound = peekdata; catch sound = zeros); end warning; sound = sound - mean); % Center about the mean. soundMax = max)); % Calculate max deviation from mean. timePrevious = timeCurrent; % Make our figure current. figOld = get; %if fig ~= figOld %set; %end %plot;         imageDifference = abs;         imageMax = max);         imagePrevious = imageCurrent;         figOld = get;         if fig ~= figOld             set;         end         image;         label = datestr;         xlabel;         set;         if imageMax > motionThreshold             motion = true;         else             motion = false;         end         if ~islogging             % 如果有东西闯入,进行响应             if motion                 % 将figure变红                 set;                 % 将闯入者的视频发送到你的邮箱                 str=['SB.jpg'];                 imwrite; %保存拍下来的图像                 customIntruderAction();             else                 set;             end;         end     if ~islogging    % if noise       customIntruderAction();%有人闯入时发进行报警         if fig ~= figOld             set;         end    % end     end    %function customIntruderAction()      % [y,fs]=wavread;      % sound; end     function figureCloseFcn         %关闭视频和声音         try             stop;             stop;         catch         end         closereq;     end     function figureDeleteFcn         delete;         delete;     end end 纯M文件编程。
目录 第1章 matlab概述. 1.1 matlab的发展历程 1.2 matlab产品组成及语言特点 1.2.1 matlab的主要产品构成 1.2.2 matlab语言的特点 1.3 matlab 7.0的新功能和新产品 1.3.1 matlab 7.0的新功能 1.3.2 matlab升级及新增的模块 1.4 小结 第2章 matlab程序设计及代码优化 2.1 matlab的表达式和变量 2.1.1 表达式 2.1.2 变量 2.2 细胞数组与结构数组 2.2.1 细胞数组 2.2.2 结构数组 2.3 类与对象 2.4 流程控制 2.4.1 for循环结构 2.4.2 while循环结构 .2.4.3 if-else-end分支结构 2.4.4 switch-case结构 2.4.5 try-catch结构 2.5 m文件编程 2.6 m文件编程规范 2.7 m文件评述器 2.8 提高m文件执行效率的技巧 2.8.1 矢量化操作 2.8.2 给数组预定义维 2.8.3 下标或者索引操作 2.8.4 尽量多使用函数文件而少使用非脚本文件 2.8.5 将循环体的内容转换为c-mex 2.8.6 内存优化 2.9 小结 第3章 matlab混合编程简介 3.1 进行混合编程的出发点 3.2 matlab应用程序接口简介 3.3 几种常见的混合编程方法简介 3.3.1 使用matlab自带的matlab compiler 3.3.2 利用matlab引擎 3.3.3 利用activex控件 3.3.4 利用mat文件 3.3.5 c-mex 3.3.6 利用mideva/matcom 3.3.7 利用matrix[lib]实现混合编程 3.3.8 利用matlab add-in 3.3.9 matlab com builder 3.3.10 matlab和excel混合编程 3.4 小结 第4章 c-mex编程 4.1 c-mex简介 4.2 mex文件系统的配置 4.3 mex文件的结构和运行 4.3.1 mex文件结构 4.3.2 mex函数的执行流程 4.3.3 mex文件的结构和使用 4.3.4 mex文件与独立应用程序的区别 4.4 c语言mex函数 4.5 c-mex混合编程 4.6 visual c++mex文件的建立和调试 4.6.1 visual c++mex程序的建立和环境设置 4.6.2 mex程序的调试 4.6.3 mex独立应用程序的发布 4.7 mex编程实例 4.8 小结 第5章 通过matlab引擎实现混合编程 5.1 matlab引擎简介 5.2 matlab引擎库函数 5.3 visual c++调用matlab引擎时的环境设置 5.4 matlab引擎类的封装 5.4.1 cmatlabeng类的定义和实现代码 5.4.2 cmatlabeng说明 5.4.3 cmatlabeng说明和使用方法 5.5 应用实例 5.6 小结 第6章 mat文件实现数据共享 6.1 mat文件简介 6.2 操作mat文件 6.2.1 mat文件格式 6.2.2 操作mat文件的matlab api 6.3 visual c++调用mat时的环境设置 6.4 实例 6.5 小结 第7章 利用mideva实现混合编程 7.1 mideva简介 7.2 mideva的安装 7.3 mideva环境下m文件到dll/exe文件的转换 7.4 visual c++环境下使用mideva混合编程 7.4.1 混合编程环境的设置 7.4.2 通过外壳函数调用 7.5 matrix[lib] 7.6 混合编程实例 7.7 小结 第8章 利用matrix[lib]实现混合编程 8.1 matrix[lib]简介 8.2 matrix[lib]与visual c++混合编程 8.2.1 matrix[lib]的安装 8.2.2 visual c++环境配置 8.2.3 初始化库 8.3 matrix[lib]函数使用参考 8.3.1 矩阵操作 8.3.2 库常量 8.3.3 访问库函数 8.3.4 矩阵i/o 8.3.5 图形函数 8.4 混合编程实例 8.5 matlab数学库 8.5.1 简介 8.5.2 visual c++工程调用matlab数学函数库的环境设置 8.6 小结.. 第9章 通过matlab add-in实现混合编程 9.1 matlab add-in简介 9.2 matlab add-in安装和在visual c++的环境设置 9.3 通过matlab add-in生成独立应用程序 9.4 matlab add-in实例 9.5 小结 第10章 matlab和delphi混合编程 10.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值