Hiển thị kết quả từ 1 đến 3 / 3
  1. #1
    Tham gia
    27-06-2009
    Bài viết
    10
    Like
    0
    Thanked 0 Times in 0 Posts

    Dùng MATLAB engine trong C++

    Hi all
    Xin nhờ các bạn chỉ giúp 1 số việc, khi dùng các thư viện của MATLAB để lập trình C++. mình tạo/cóp file .dll từ thư viện MATLAB để dùng trong C++ có vấn đề. Đã google rồi mà vẫn không giải quyết được.

    Cụ thể là đã có được file .dll rồi, dưng mà khi gọi hàm mxDoubleMatrixCreate thì nó lại nói là không tìm thấy entrypoint của hàm này trong file .dll; mình dùng phần mềm depends để track thì thấy rõ ràng cái hàm đó trong file .dll, không hiểu fix kiểu gì ?

    Lên 1 số trang thì nó bảo là đường dẫn phải đặt ở chỗ khác (ghi trong depends.exe, mình đã copy file làm 2 đặt vào cả 2 chỗ rồi vẫn không fix được)

    Bạn nào biết cách giải quyết xin chỉ giùm thanks nhiều

    Code:
    /* $Revision: 1.1.6.2 $ */
    /*
     *	engdemo.cpp
     *
     *	This is a simple program that illustrates how to call the MATLAB
     *	Engine functions from a C++ program.
     *
     * Copyright 1984-2007 The MathWorks, Inc.
     * All rights reserved
     */
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include "engine.h"
    #pragma comment (lib,"C:/Program Files/MATLAB/R2009a/extern/lib/win32/microsoft/libeng.lib")
    #pragma comment (lib,"C:/Program Files/MATLAB/R2009a/extern/lib/win32/microsoft/libmx.lib")
    #pragma comment (lib,"C:/Program Files/MATLAB/R2009a/extern/lib/win32/microsoft/libut.lib")
    #define  BUFSIZE 256
    
    int main()
    
    {
    	Engine *ep;
    	mxArray *T = NULL, *result = NULL;
    	char buffer[BUFSIZE+1];
    	double time[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 locally by executing the string
    	 * "matlab"
    	 *
    	 * To start the session on a remote host, use the name of
    	 * the host as the string rather than \0
    	 *
    	 * For more complicated cases, use any string with whitespace,
    	 * and that string will be executed literally to start MATLAB
    	 */
    	if (!(ep = engOpen("\0"))) {
    		fprintf(stderr, "\nCan't start MATLAB engine\n");
    		return EXIT_FAILURE;
    	}
    
    	/*
    	 * PART I
    	 *
    	 * For the first half of this demonstration, we will send data
    	 * to MATLAB, analyze the data, and plot the result.
    	 */
    
    	/* 
    	 * Create a variable for our data
    	 */
    	T = mxCreateDoubleMatrix(1, 10, mxREAL);
    	memcpy((void *)mxGetPr(T), (void *)time, sizeof(time));
    	/*
    	 * Place the variable T into the MATLAB workspace
    	 */
    	engPutVariable(ep, "T", T);
    
    	/*
    	 * Evaluate a function of time, distance = (1/2)g.*t.^2
    	 * (g is the acceleration due to gravity)
    	 */
    	engEvalString(ep, "D = .5.*(-9.8).*T.^2;");
    
    	/*
    	 * Plot the result
    	 */
    	engEvalString(ep, "plot(T,D);");
    	engEvalString(ep, "title('Position vs. Time for a falling object');");
    	engEvalString(ep, "xlabel('Time (seconds)');");
    	engEvalString(ep, "ylabel('Position (meters)');");
    
    	/*
    	 * use fgetc() to make sure that we pause long enough to be
    	 * able to see the plot
    	 */
    	printf("Hit return to continue\n\n");
    	fgetc(stdin);
    	/*
    	 * We're done for Part I! Free memory, close MATLAB figure.
    	 */
    	printf("Done for Part I.\n");
    	mxDestroyArray(T);
    	engEvalString(ep, "close;");
    
    
    	/*
    	 * PART II
    	 *
    	 * For the second half of this demonstration, we will request
    	 * a MATLAB string, which should define a variable X.  MATLAB
    	 * will evaluate the string and create the variable.  We
    	 * will then recover the variable, and determine its type.
    	 */
    	  
    	/*
    	 * Use engOutputBuffer to capture MATLAB output, so we can
    	 * echo it back.  Ensure first that the buffer is always NULL
    	 * terminated.
    	 */
    
    	buffer[BUFSIZE] = '\0';
    	engOutputBuffer(ep, buffer, BUFSIZE);
    	while (result == NULL) {
    	    char str[BUFSIZE+1];
    	    /*
    	     * Get a string input from the user
    	     */
    	    printf("Enter a MATLAB command to evaluate.  This command should\n");
    	    printf("create a variable X.  This program will then determine\n");
    	    printf("what kind of variable you created.\n");
    	    printf("For example: X = 1:5\n");
    	    printf(">> ");
    
    	    fgets(str, BUFSIZE, stdin);
    	  
    	    /*
    	     * Evaluate input with engEvalString
    	     */
    	    engEvalString(ep, str);
    	    
    	    /*
    	     * Echo the output from the command.  First two characters are
    	     * always the double prompt (>>).
    	     */
    	    printf("%s", buffer+2);
    	    
    	    /*
    	     * Get result of computation
    	     */
    	    printf("\nRetrieving X...\n");
    	    if ((result = engGetVariable(ep,"X")) == NULL)
    	      printf("Oops! You didn't create a variable X.\n\n");
    	    else {
    		printf("X is class %s\t\n", mxGetClassName(result));
    	    }
    	}
    
    	/*
    	 * We're done! Free memory, close MATLAB engine and exit.
    	 */
    	printf("Done!\n");
    	mxDestroyArray(result);
    	engClose(ep);
    	
    	return EXIT_SUCCESS;
    }
    Quote Quote

  2. #2
    Tham gia
    01-04-2012
    Bài viết
    1
    Like
    0
    Thanked 0 Times in 0 Posts
    Không biết bạn có cần thông tin giải đáp nữa hay không? Nhưng vẫn giải thích cho những ai khi search google thì cũng biết hướng giải quyết

    Bạn search google "Kết nối C++ với Matlab" thì ngay ở kết quả đầu tiên là cách hướng dẫn kết nối (file PDF down về). Ở trong đó có hướng dẫn khá chi tiết.

  3. #3
    Tham gia
    10-12-2004
    Location
    HCMC
    Bài viết
    2,121
    Like
    283
    Thanked 720 Times in 362 Posts
    không có sẵn ở đây để test thử, nhưng khi bạn compile thì có lỗi gì không, sau khi compile xong nó sẽ link lại thành file exe, có thể dll được compile theo kiểu C còn ctr của bạn thì dịch theo kiểu C++, bạn phải ép nó dịch theo kiểu C, thêm tham số /c vô thử coi sao.
    nhân tiện, copy đầy đủ cái lỗi lên thử, và chạy command dumpbin /exports <file dll> rồi post cái dòng có function bạn cần xài lên luôn nha.

Bookmarks

Quy định

  • Bạn không thể tạo chủ đề mới
  • Bạn không thể trả lời bài viết
  • Bạn không thể gửi file đính kèm
  • Bạn không thể sửa bài viết của mình
  •