standalone MATLAB program: retrieve path and filename

2 views (last 30 days)
I am using MATLAB R2008b in Windows 7. The compiler is Lcc-win32 C 2.4.1. I tried to make an exe file from an m file which can retrieve its path and filename. A colleague gave me a C file (mlgetmodulefilename.c) and suggested me to use mcc -m XYZ.m mlgetmodulefilename.c to generate XYZ.exe
First, I got some errors regarding matlab.h. So I copied, pasted, and renamed mex.h to matlab.h. Then, I faced the following error and warning:
Warning: Name is nonexistent or not a directory: C:\Program Files\MATLAB\R2008b\toolbox\compiler\patch.
c:\users\user\appdata\local\temp\mbuild~1\mlgetmodulefilename.obj .text: undefined reference to '_mxCreateString'
Can anybody help me with this? Thank you.
mlgetmodulefilename.c
#include "matlab.h"
#include <stdio.h>
#include <windows.h>
#include "megetmodulefilename_external.h"
/*
INPUT
None
OUTPUT
None
RETURN
The full filename of the calling function
SYNOPSIS
*/
#define BUFSIZE 255
mxArray * Mmegetmodulefilename(int nargout_)
{
DWORD buflen;
TCHAR lpFilename[BUFSIZE]=TEXT("");
buflen = GetModuleFileName(NULL, lpFilename, BUFSIZE);
if (buflen == 0) {
return (mxCreateString("") );
}
else {
return (mxCreateString(lpFilename) );
}
}

Answers (3)

Titus Edelhofer
Titus Edelhofer on 30 Apr 2015
Hi Kevin,
I'm not sure what the getmodulefilename is supposed to do,and even less, why you want to incorporate into your standalone executable?
Titus

Kevin
Kevin on 30 Apr 2015
Hi Titus: In 2002, I wrote an m file and compiled it into a standalone application. Suppose the m file is called XYZ.m and the application is XYZ.exe. Since different users may store XYZ.exe in different directories, I wanted XYZ.exe to be able to retrieve the path it is stored. This is because XYZ.exe has to take some user input through external files, which are supposed to be stored in the same directory. So, knowing the path, XYZ.exe can be directed to read those external files. A colleague gave me two c files: megetmodulefilename.c and mlgetmodulefilename.c. I created XYZ.exe by compiling it in MATLAB as follows
mcc -m XYZ.m mlgetmodulefilename.c
In XYZ.m, this line is used to retrieve the path
ProgramDir=fileparts(megetmodulefilename);
While mlgetmodulefilename.c is attached above, megetmodulefilename looks like
#ifndef MLF_V2
#define MLF_V2 1
#endif
#ifndef __megetmodulefilename_external_h
#define __megetmodulefilename_external_h 1
#include "matlab.h"
extern mxArray * Mmegetmodulefilename(int nargout_);
#endif
After 13 years, I find some bugs in XYZ.m, so I decide to modify it and re-compile it to XYZ.exe. That is where I see the warning and error message. Could you please give me a hand?

Kevin
Kevin on 4 May 2015
Problem solved. See link if you have the same problem.

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!