如何在Matlab GUI(使用MCR部署)和C ++代码之间共享数据?

时间:2013-07-09 08:05:32

标签: c++ matlab user-interface matlab-compiler data-sharing

我在Linux 64位上工作,我想在C ++代码中使用GUI(使用Matlab创建并部署为共享库,这要归功于MCR),我的微积分函数在哪里。

问题是在GUI和C ++代码之间共享数据。

我在C ++中的两个不同的线程中隔离了GUI和微积分函数,并且我已经能够从GUI中编写一个命名管道(在点击一个按钮切换回调之后),而C ++是读数。

因此,我得到了数据给微积分函数,但是,当GUI要读取时,所有数据都被阻止。

以下是我的代码的一些片段:

我的GUI(C ++)线程:

static void *fn_gui(void *p_data)
{
    std::cout << "the gui should launch now" << std::endl;

    /* Call of the Matlab Gui*/ 
    gui();

    mclWaitForFiguresToDie(NULL);

    return NULL;
}

我的微积分(C ++):

static void *fn_calculus(void *p_data)
{
    mkfifo("mypipe1",S_IRWXU);
    mkfifo("mypipe2",S_IRWXU);  

    /*****************/
    /*   READING     */
    /*****************/

    /* Declaration of the input structures of the calculus functions */
    const char * in_fieldnames[] = {"x","y"};

    mwArray a(1,1,2,in_fieldnames);
    mwArray b(1,1,2,in_fieldnames);
    mwArray c(1,1,2,in_fieldnames);

    /* Opening of the first fifo */
    int mypipe1=open("mypipe1",O_RDONLY);

    /* Determination of the length of the length of the data (1st character of the message) */
    char * lc;
    lc = (char*) malloc(sizeof(char));
    read(mypipe1,lc,1);
    int l = atoi(lc);

    /* Determination of the length of the data (the l following characters) */
    char * Lc;
    Lc = (char*) malloc(l*sizeof(char));
    read(mypipe1,Lc,l);
    int L = atoi(Lc);

    /* Memory allocation of the message and storage of the data (the L following characters)*/
    char * message1;
    message1 = (char*) malloc(L*sizeof(char));
    read(mypipe1,message1,L);
    close(mypipe);

    std::cout << "message = " << message1 << std::endl;

    /* Formatting of the data */
    mwArray flow1(message1);
    mwArray data;

    split(1,data,flow1);  /* Put the string "a:b:c:d" under the "a" "b" "c" "d" form */
                    /* temporary Matlab deployed function to improve */

    /*****************/
    /*   CALCULUS    */
    /*****************/

    /* Filling of the input structures */
    a.Get(1,1).Set(data.Get(1,1));
    a.Get(1,2).Set(data.Get(1,2));mkf
    b.Get(1,1).Set(data.Get(1,3));
    b.Get(1,2).Set(data.Get(1,4));

    /* Call to the Matlab Calculus Function (it fills c) */
    minpyt(1,c,a,b);

    std::cout << "c = " << c << std::endl;

    /* Formatting of the data */

    double result[2];

    c.Get("x",1,1).GetData(&result[0],1);
    c.Get("y",1,1).GetData(&result[1],1);

    int length=sizeof(result[0])+sizeof(result[1])+1;  /* length of the two figures plus the extra ':' character between them */
    int length2 = (int) log10(length) + 1; /* number of digits of the previous length */

    char message2[length+length2+1]; /* length for the message and the 2 lengths */

    sprintf(message2,"%d%d%f:%f",length2,length,result[0],result[1]);


    /*****************/
    /*   WRITING     */
    /*****************/

    int mypipe2 = open("mypipe2",O_WRONLY);

    write(mypipe2,message2,sizeof(message2));

    close(mypipe2);

    return NULL;
}

我的GUI回调(Matlab):

function buttonCallback(gcbf,data)

    % Getting the data from the GUI
    data = guidata(gcbf);

    a.x = get(data.Ax,'String');
    a.y = get(data.Ay,'String');
    b.x = get(data.Bx,'String');
    b.y = get(data.By,'String');

    guidata(gcbf,data);

    % Formatting the data

    message = [a.x ':' a.y ':' b.x ':' b.y];
    L = num2str(length(message));
    l = num2str(length(L));

    message = [l L message];

    % WRITING

    mypipe1 = fopen('mypipe1','w');
    fwrite(mypipe1,message,'char');
    fclose(pipe);

    % READING

    mypipe2 = fopen('mypipe2','r');
    l = fread(mypipe2,1,'double');
    L = fread(mypipe2,l,'double');
    flow = fread(mypipe2,L,'*char');
    fclose(mypipe2);

    fprintf(1,'message read = %s\n',flow);

    % Formatting the data

    message = regexp(flow,':','split');

    c.x = str2double(message{1,1});
    c.y = str2double(message{1,2});

    % Updating the GUI

    set(data.Cx,'String',c.x);
    set(data.Cy,'String',c.y);

    plot(data.axes,[a.x;b.x;c.x;a.x],[a.y;b.y;c.y;a.y],'r-');

    axis equal;

end

当我点击按钮时(在填充a和b的值之后),GUI调用回调函数。 C ++的主要功能只是初始化MCR和正确的库并启动线程。

如果有人有想法。

谢谢和问候

0 个答案:

没有答案