matlab c++ 画图【转载】

本文介绍如何解决在使用MATLAB Compiler生成的应用中,因MCR终止而导致图像残留的问题。通过修改MATLAB和C++代码实现图像正常显示及等待用户交互后正确关闭。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文:http://blog.youkuaiyun.com/owldestiny/article/details/4210177



最近有朋友问我他照着我之前的一篇文章(http://blog.youkuaiyun.com/owldestiny/archive/2009/03/07/3966340.aspx )

做画图的时候画不出来图像,不知道如何解决,自己试了一下,搞定了在这里贴出来

首先matlab文件他是这样写的:

[cpp]  view plain copy
  1. function mfun()  
  2. x=-10:0.1:10;  
  3. y=sin(x);  
  4. plot(x,y)  

C++代码是:

[cpp]  view plain copy
  1. int main()  
  2. {  
  3.     cout<<"test";  
  4.     drawInitialize();  
  5.     mfun();  
  6.     drawTerminate();//terminate  
  7.     return 0;  
  8. }  

C++代码最后一句建议加上system("pause");

这样如果程序运行过快最后还是能停下等待按键,就可以看到程序运行的结果;

加上之后控制台中报错为:

testWarning: 1 visible figure(s) exist at MCR termination.
If your application has terminated unexpectedly, please note that
 applications generated by the MATLAB Compiler terminate when there are no
 visible figure windows. See the documentation for WaitForFiguresToDie and
 WAITFORCALLBACKS for more information.
Warning: Class
    'graph2d.lineseries'
in use at MCR termination.
If your application has terminated unexpectedly, please note that
 applications generated by the MATLAB Compiler terminate when there are no
 visible figure windows. See the documentation for WaitForFiguresToDie and
 WAITFORCALLBACKS for more information.
Warning: Objects of graph2d.lineseries class exist - not clearing this class
or any of its super-classes

大致的意思就是图片在退出MCR时仍然存在,这个是不允许的,terminate时MCR就不存在了,图片也不可能继续显示,

解决的方法是将MCR存在的时间延长,可以在Matlab代码中加入延时程序或等待按键代码,我采用的是等待按键,

[cpp]  view plain copy
  1. function [t]=draw()  
  2. x=-10:0.1:10;  
  3. y=sin(x);  
  4. figure(1)  
  5. plot(x,y)  
  6. hold on  
  7. t=1;  
  8. w=1;  
  9. while w,  
  10.     w=waitforbuttonpress  
  11. end  

最后一行就是等待鼠标按键,w变为0,循环终止,draw函数终止,MCR退出;

 

C++的代码也进行了一点修改,

[c-sharp]  view plain copy
  1. #pragma comment(lib,"draw.lib")  
  2. #include "draw.h"  
  3. #include <iostream>  
  4. using namespace std;  
  5. int main()  
  6. {  
  7.     cout<<"test";  
  8.     drawInitialize();  
  9.     short int nargout = 1;   
  10.     mwArray t(1,1,mxINT8_CLASS);  
  11.     draw(nargout, t);  
  12.     drawTerminate();//terminate  
  13.     system("pause");  
  14.     return 0;  
  15. }  

这样就可以实现了,具体代码在这里(http://download.youkuaiyun.com/source/1341025 )可以下载到,,欢迎大家与我讨论,mailto:chen0510566@163.com


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值