亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: C++調(diào)用MATLAB引擎 [打印本頁(yè)]

作者: keehoo    時(shí)間: 2011-12-20 12:19
標(biāo)題: C++調(diào)用MATLAB引擎
這是MATLAB自帶的例子,我在WindowsXP(SP2), MATLAB R2007b, VC6.0下實(shí)現(xiàn)。

具體的步驟是:

1). 設(shè)置系統(tǒng)環(huán)境變量path,path里面要包括MATLAB的exe, dll那個(gè)路徑

%MATLAB_PATH%\bin\win32

只要MATLAB是正確安裝,它應(yīng)該會(huì)自動(dòng)配置path環(huán)境變量的。

右擊我的電腦->屬性->高級(jí)->環(huán)境變量->系統(tǒng)變量

雙擊path

path值應(yīng)該有以下兩個(gè)值,中間是分號(hào)

d:\MATLABR2007b\bin;d:\MATLABR2007b\bin\win32

2). 設(shè)置VC的路徑

運(yùn)行VC,轉(zhuǎn)到options

include增加MATLAB的include路徑 %MATLAB_PATH%\extern\include

lib增加%MATLAB_PATH%\extern\lib\win32\microsoft

這一步以后都不用做了。

3). 在VC中新建一個(gè)console項(xiàng)目(也可以是win32,mfc等其他項(xiàng)目),項(xiàng)目的link輸入增加libeng.lib和libmx.lib(每個(gè)工程都要添加,否則編譯不能通過(guò))

4). main函數(shù)的代碼:
  1. /* $Revision: 1.8.4.1 $ */
  2. /*
  3. * engdemo.c
  4. *
  5. * This is a simple program that illustrates how to call the MATLAB
  6. * Engine functions from a C program.
  7. *
  8. * Copyright 1984-2003 The MathWorks, Inc.
  9. * All rights reserved
  10. */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "engine.h"
  15. #define BUFSIZE 256

  16. int main()

  17. {
  18. Engine *ep;
  19. mxArray *T = NULL, *result = NULL;
  20. char buffer[BUFSIZE+1];
  21. double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };

  22. /*
  23. * Start the MATLAB engine locally by executing the string
  24. * "matlab"
  25. *
  26. * To start the session on a remote host, use the name of
  27. * the host as the string rather than \0
  28. *
  29. * For more complicated cases, use any string with whitespace,
  30. * and that string will be executed literally to start MATLAB
  31. */
  32. if (!(ep = engOpen("\0"))) {
  33.    fprintf(stderr, "\nCan't start MATLAB engine\n");
  34.    return EXIT_FAILURE;
  35. }

  36. /*
  37. * PART I
  38. *
  39. * For the first half of this demonstration, we will send data
  40. * to MATLAB, analyze the data, and plot the result.
  41. */

  42. /*
  43. * Create a variable for our data
  44. */
  45. T = mxCreateDoubleMatrix(1, 10, mxREAL);
  46. memcpy((void *)mxGetPr(T), (void *)time, sizeof(time));
  47. /*
  48. * Place the variable T into the MATLAB workspace
  49. */
  50. engPutVariable(ep, "T", T);

  51. /*
  52. * Evaluate a function of time, distance = (1/2)g.*t.^2
  53. * (g is the acceleration due to gravity)
  54. */
  55. engEvalString(ep, "D = .5.*(-9.8).*T.^2;");

  56. /*
  57. * Plot the result
  58. */
  59. engEvalString(ep, "plot(T,D);");
  60. engEvalString(ep, "title('Position vs. Time for a falling object');");
  61. engEvalString(ep, "xlabel('Time (seconds)');");
  62. engEvalString(ep, "ylabel('Position (meters)');");

  63. /*
  64. * use fgetc() to make sure that we pause long enough to be
  65. * able to see the plot
  66. */
  67. printf("Hit return to continue\n\n");
  68. fgetc(stdin);
  69. /*
  70. * We're done for Part I! Free memory, close MATLAB engine.
  71. */
  72. printf("Done for Part I.\n");
  73. mxDestroyArray(T);
  74. engEvalString(ep, "close;");


  75. /*
  76. * PART II
  77. *
  78. * For the second half of this demonstration, we will request
  79. * a MATLAB string, which should define a variable X. MATLAB
  80. * will evaluate the string and create the variable. We
  81. * will then recover the variable, and determine its type.
  82. */
  83.    
  84. /*
  85. * Use engOutputBuffer to capture MATLAB output, so we can
  86. * echo it back. Ensure first that the buffer is always NULL
  87. * terminated.
  88. */

  89. buffer[BUFSIZE] = '\0';
  90. engOutputBuffer(ep, buffer, BUFSIZE);
  91. while (result == NULL) {
  92.      char str[BUFSIZE+1];
  93.      /*
  94.       * Get a string input from the user
  95.       */
  96.      printf("Enter a MATLAB command to evaluate. This command should\n");
  97.      printf("create a variable X. This program will then determine\n");
  98.      printf("what kind of variable you created.\n");
  99.      printf("For example: X = 1:5\n");
  100.      printf(">> ");

  101.      fgets(str, BUFSIZE, stdin);
  102.    
  103.      /*
  104.       * Evaluate input with engEvalString
  105.       */
  106.      engEvalString(ep, str);
  107.      
  108.      /*
  109.       * Echo the output from the command. First two characters are
  110.       * always the double prompt (>>).
  111.       */
  112.      printf("%s", buffer+2);
  113.      
  114.      /*
  115.       * Get result of computation
  116.       */
  117.      printf("\nRetrieving X...\n");
  118.      if ((result = engGetVariable(ep,"X")) == NULL)
  119.        printf("Oops! You didn't create a variable X.\n\n");
  120.      else {
  121.    printf("X is class %s\t\n", mxGetClassName(result));
  122.      }
  123. }

  124. /*
  125. * We're done! Free memory, close MATLAB engine and exit.
  126. */
  127. printf("Done!\n");
  128. mxDestroyArray(result);
  129. engClose(ep);

  130. return EXIT_SUCCESS;
  131. }

復(fù)制代碼
編譯運(yùn)行,等一會(huì)兒會(huì)有一個(gè)MATLAB圖形窗口彈出。

轉(zhuǎn)載自:http://hi.baidu.com/dreadnaught/ ... 48a5c97dd92a3f.html




歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2