⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sosmac.c

📁 《精通matlab7》“mastering matlab 7”的代码。
💻 C
字号:
/* *  sos.c - Calculate the sum of the squares of the elements of a vector. * *  Mastering MATLAB 7 Engine Example 1 * */#include <stdio.h>#include <string.h>#include "engine.h"#define  BUFSIZE 256int main(){    Engine *mat;    mxArray *mydata = NULL, *sqdata = NULL;    int i;    double x, *myptr, *sptr;    char buf[BUFSIZE];    double dataset[10] = { 0.0, 1.0, 2.0, 3.0, 4.0,                            5.0, 6.0, 7.0, 8.0, 9.0 };    /* Start the MATLAB engine on the local computer.  *//*    if (!(mat = engOpen("\0"))) { */    if (!(mat = engOpen("/Applications/MATLAB7/bin/matlab"))) {        fprintf(stderr, "\nCannot open connection to MATLAB!\n");        return EXIT_FAILURE;    }    /* Create an mxArray and get a C pointer to the mxArray. */    mydata = mxCreateDoubleMatrix(1, 10, mxREAL);    myptr = mxGetPr(mydata);    /* Copy the dataset array to the new mxArray. */    memcpy((void *)myptr, (void *)dataset, sizeof(dataset));    /* Pass the mxArray to the engine and square the elements. */    engPutVariable(mat, "newdata", mydata);    engEvalString(mat, "sqdata = newdata.^2");    /* Create an output buffer to capture MATLAB text output. */    engOutputBuffer(mat, buf, BUFSIZE);    /* Calculate the sum of the squares and save the result in x. */    engEvalString(mat,"disp(sum(sqdata))");    x=atof(buf+2);    /* Retrieve the array of squares from the emgine, */    if ((sqdata = engGetVariable(mat,"sqdata")) == NULL) {        fprintf(stderr, "Cannot retrieve sqdata!\n\n");        return EXIT_FAILURE;    }        /* and get a C pointer to the mxArray. */    sptr = mxGetPr(sqdata);    /* Print the results to stdout. */    printf("\nThe inputs are:\n");    for (i=0;i<10;i++)      printf("%6.1f ",myptr[i]);    printf("\n\nThe squares are:\n");    for (i=0;i<10;i++)      printf("%6.1f ",sptr[i]);    printf("\n\nThe sum of the squares is %6.1f \n\n",x);    /* Free the mxArray memory and quit MATLAB. */    mxDestroyArray(mydata);    mxDestroyArray(sqdata);    engClose(mat);        return EXIT_SUCCESS;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -