I have a function on image processing written in Matlab. I want to convert it into an executable file so that I can use that function in other applications. I tried Matlab coders, but since I'm using a Trial version, I don't have the Matlab Coder. What can be done?
Can the same code written in Octave be converted into an executable file?
img2=rgb2gray(i1);
imshow(img2);
img3=imtophat(img2,strel('disk',15));
img4=imadjust(img3);
layer=img4(:,:,1);
img5=layer>100 & layer<250;
img6=imfill(img5,'holes');
img7=bwareaopen(img6,5);
[L,ans]=bwlabeln(img7);
imshow(img7);
I=imread(i1);
Ians=CarDentIdentification(I);
% Display the original image and the segmented image side-by-side.
imshowpair(I,Ians,'montage')
I'm not sure how well this works on Windows or Mac but on UNIX systems, I've been able to call my GNU octave scripts as executables by adding this line to the top of my octave script
#!/usr/bin/octave
The "#!" gets picked up by your OS's program loader, and indicates that the following file is actually a script. The loader then looks to the given file path for the location of the interpreter. If your Octave interpreter is not at "/usr/bin/octave", you'd replace that section with the correct location.
After that you need to tell your OS that this script is an executable file. In bash you do that with this command.
chmod +x octaveFile.m
This let's you call your octave script just like any other executable. In my case, I then called my scripts from python using subprocess.call('octaveFile.m').
Note that this is not a MATLAB or Octave feature. This is done by the operating system, so results may vary.
Related
We're using the latest version of CMake(3.17), trying to compile a top level .m file into an executable on Matlab 2015b
I've done
find_package(Matlab OPTIONAL_COMPONENTS MEX_COMPILER MCC_COMPILER)
and it finds matlab. I'm able to make MEX files using matlab_add_mex() (and access them from matlab), but the next step is to take the top level .m file and compile it to an executable so it can be sent off to a compute farm to run very large input vectors.
At the commandline, we just do mcc -m simulation.m -I <required include paths> etc and it works. I want this to work in our CMake build environment with dependencies properly calculated (as best I can)
I assume I need to use add_custom_command() like what is mentioned here (https://gitlab.kitware.com/cmake/cmake/issues/18183), and I assume I need to calculate the dependencies correctly.
Using this cme.exe line, I can get the dependencies out to a file:
matlab -wait -nosplash -nojvm -nodesktop -r "[fList,~] = matlab.codetools.requiredFilesAndProducts('simulation.m');;for file = fList;fprintf(2,'%s\n',file{:});end;exit" -logfile output.txt
Is there a better way to do this? This ends up launching matlab and blopping the output to a file, rather than stdout or whatever. It's pretty dang slow to start up, as well.
I'm contemplating writing a python script or C program that embeds matlab that gets the information then prints it to stdout or just using mcc on the script that is supposed to calculate the dependencies, but that seems really, really convoluted for something that seems like a standard step in any repeatable build process.
So, any pointers? I can't find a way to get the dependencies except inside a matlab command line, and I'd hope that CMake had m->exe (in addition to mex) in its wheelhouse.
fwiw, I took the following and put it into a file get_dependencies.m:
function print_dependencies(filename)
[fList,~] = matlab.codetools.requiredFilesAndProducts(filename)
for file = fList
fprintf(1,'%s\n',file{:})
end
end
and compiled it using
mcc -m get_dependencies.m
And when it runs, it whines about undefined variables:
Undefined variable "matlab" or class "matlab.codetools.requiredFilesAndProducts".
I am trying to profile a CUDA code that is embedded in a mex file, called from MATLAB. Running on a win7 x64 machine, MATLAB r2014b
The code is structured as follows:
MATLAB test.m
->contains some standard code generating variables (and calling 1 or 2 minor own MATLAB fucntions)
-> calls testcuda.mex
->contains small, standard, no library C++ code
-> calls either test1.cu or test2.cu
->.cu files end in cudaDeviceReset();
I did the following, as stated in several places on the internet:
Compiled mex files. Test them. They work.
Add exit in the end of test.m.
Launch the NVIDIA Visual Profiler. File -> New Session.
add the full path of the Matlab executable file, for example C:\Program Files\MATLAB\R2014b\bin\matlab.exe
Working directory: add the full path of the Matlab .m file. C:\CUDA_MATLABtests\MyToolbox
Arguments: -nojvm -nosplash -r test
However, when I run the profiler,I get
======== Warning: No CUDA application was profiled, exiting
And nothing more.
I am missing some instruction?
Is there any particular code structure that would make the profiler not to profile?
A Matlab installation comes with two binaries, a launcher in <matlabroot>\bin and the main application in <matlabroot>\bin\<arch>. The later is the executable which also executes the mex functions in it's address space.
When a debugging tool needs to start the application directly, you have to start the one in <matlabroot>\bin\<arch>.
When a debugging tool automatically attaches to child processes as well, it is typically sufficient to start the binary in <matlabroot>\bin setting the -wait parameter. Seeing the launcher application terminating, debugging tools often stop.
Never use the binary in <matlabroot>\bin\<arch> directly unless you have to.
I am trying to compile a Matlab function using the mcc function. My function writes a line of text to a file.
function hello()
ofid = fopen('hello.txt','w');
fprintf(ofid, 'Hello there, this is matlab.\n');
fclose(ofid);
end
It runs as expected when called from the Matlab console. However, when I compile the file to an executable using mcc
/opt/matlab/matlab2008a/bin/mcc -m hello.m
and run it as
./hello
It seems to run forever and produces no hello.txt file. Both the executable as well as the Matlab function files are in the Matlab working directory.
I am unable to see what can be possibly wrong in my use of mcc. Please help.
Typically, you need to setup the MATLAB runtime environment when executing mcc compiled applications.
You'll probably get more mileage the first time by using deploytool (from the command line) to compile your hello.m program here. MATLAB's application builder will produce a run_hello.sh file that you can peruse through to see how they setup their libraries. Then, to execute your program you would actually call it like this:
./run_hello.sh /Applications/MATLAB/MATLAB_Runtime/v90/
Note: I have just included a generic location for the MATLAB runtime environment, but the actual location will vary depending on your platform and version of MATLAB you built the application with.
I encountered a very strange problem when using the SYSTEM function to call an executable file from Matlab.
First, I can run the executable file in a window console with no problem. However, when I call system(foo.exe) in Matlab, it does nothing but return the status value -1.0737e+09, and it does not throw any warnings or errors.
I am using Matlab R2009b on a 32-bit windows system and the executable file depends on OpenCV2.40.
The system path variable definitely include all the required lib directories. I also tried adding the relevant paths in the LD_LIBRARY_PATH, and copying the dlls in the working directory. These attempts do not change anything.
I also tried calling SYSTEM('cmd') from Matlab, and running the executable in the invoked window console. It gave me no response, either. It seems like the running environment of the console called from Matlab is different from the original window console.
Running the same code on a different machine in Matlab is successful.
I had this exact same problem. It turned out I simply needed to restart Matlab. I had not restarted Matlab after installing OpenCV2.1.
I have problems running a program compiled with MCC that uses parfor. The non-compiled .m version works (no bug). When I run the compiled version, I get
"distcomp.remoteparfor" is undefined. I believe I exactly have the error described in
the following link.
http://www.mathworks.com/support/solutions/en/data/1-PAHWE/index.html?product=CO&solution=1-PAHWE
However, since MCR is not installed on my machine (got Matlab and MCC),
I am wondering what the pathes $APPNAME_mcr/java/jar/toolbox and
$MCR/MATLAB Component Runtime/v70/java/jar/toolbox correspond to. I found
the distcomp.jar file in C:\Program Files\MATLAB\R2011b\java\jar\toolbox, but I don't know where to put it since see any path corresponding to $MCR/MATLAB Component Runtime/v70/java/jar/toolbox since MCR is not installed.
Thanks a lot!
Fred
No need to install the MCR if you are simply trying to run the application on the same machine that has MATLAB. The question to ask is, HOW are you launching the compiled application? From a DOS command window? From within MATLAB using the SYSTEM command?
If you are launching the application from a DOS / UNIX command line, then the important thing to keep in mind is that the MATLAB binaries need to be on your system path i.e. just make sure:
$MATLABROOT\bin\$ARCH
is on your system path. Where $MATLABROOT is the matlab installation folder, and $ARCH is your system architecture. For example:
c:\work\matlab\bin\win64
on my machine since my installation folder which contains MATLAB is:
c:\work
and i am on a win64 machine.
The problem is solved. The solution is to make the program
a function instead of a script :
http://www.mathworks.com/matlabcentral/answers/22825-parfor-errors-when-file-is-compiled