matlab c++ 画图【转载】

来源:互联网 发布:vscode eslint 编辑:程序博客网 时间:2024/06/04 19:04

原文:http://blog.csdn.net/owldestiny/article/details/4210177



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

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

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

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

C++代码是:

[cpp] view plaincopy
  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 plaincopy
  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 plaincopy
  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.csdn.net/source/1341025 )可以下载到,,欢迎大家与我讨论,mailto:chen0510566@163.com


0 0
原创粉丝点击