MATLAB C S-function cannot be compiled in simulink real time - simulink

I used C S-function in Simulink real time. When compiling the model, the following errors occurred,
“mxCreateDoubleScalar”is not defined;
“mexPutVariable”is not defined;
<"mxSetCell_ is_ not_ supported_ in_ Simulink_ Coder ": undeclared identifier
[enter image description here][1]
I know that Simulink real time doesn't seem to support “mxCreateDoubleScalar”、“mexPutVariable”, but I don't know how to replace it, can you help me?
[1]: https://i.stack.imgur.com/xtiyo.png

I recommend you to work with executables. First you have to have installed a C/C++ compiler. I recommend you to install minGW (you can get it on the AddOn of Matlab).
Then, you have to initialize all the variables that you are using on the s-function.
For last, when you want to make a "mex" with your model and s-function, check the command line to mex all files. I had the same problem 2 years ago and I was able to create a real-time application and run it.
Here you have how to use the mex function mex function
Here you have the information about the C/C++ compiler C/C++ compiler
Here you have the information about how configurate your .mdl or .slx Simulink Real-Time
I suggest you to post this question on the Matlab's forum, possibly they could help you better.
BR.
PD: If you have .h files linked with your .c file, make sure that on the mex command line you are linking all this files.

Related

Executing CUDA code In Matlab

I wanted to ask if anyone have run some C code containing CUDA code on Matlab?
I have read the documentation on Mathworks website but I still cant quite wrap my head around it. I do understand that it is two main type of ways you could do this either executing a CUDA kernel by constructing a object with the function parallel.gpu.CUDAKernel or by constructing a mex file out of a .cu file. There are some things though I do not understand when using these two methods.
Using the mex approach should I use another IDE like Visual Studio to compile a .cu file first before compiling the mex file in Matlab? If so how can I compile the .cu file without a main() function in the .cu file, I always get errors when I try to compile it that way in VS, or is it okay to have a main function in the .cu file and pass the pointers to the GPU arrays to the main function?
For the CUDA kernel approach, should compile the kernel in VS, and in that case how?
Both things can work.
If you want to have flexibility, my suggestion is to write your .cu files with .c (or .cpp) files*. Once you have some basic thing working, you should be able to write a mex wrapper around it in order to grab MATLAB variables and convert them co C/C++ so you can pass them to and from CUDA. This requires you to have a compiler that is both compatible with your versions of MATLAB, CUDA and OS. An example is Visual Studio 2013 in widows and most versions of MATLAB and CUDA, but please check. Generally this is done by linking nvcc to the mex compiler after setup with some xml files (see example here from my toolbox). This approach gives you full flexibility, not only for working with CUDA, but also with working with the anything that you may want to use together with your kernels e.g. tensorflow, eigen, SQL, ... Its full flexibility.
If instead you just want a few operations accelerated with simple methods, use the Parallel computing toolbox with gpuarrays for standard MATLAB operations or with parallel.gpu.CUDAKernel for your own kernels. To use this second one you need to compile a ptx file, which seems pretty straightforward. A priori this gives you less flexibility, as it will run just a kernel, but often complex GPU programs may need several kernels and data handling techniques, as well as communication between kernels etc. However I have personally haven't tried it and perhaps you can achieve full flexibility. Let me know and I will edit the answer.
In short, you choice depends on your application/needs.
*You might not need .c or .cpp files with modern versions of MATLAB and mexcuda.

How to make an executable file on matlab and run on raspberry pi 3B [duplicate]

I was wondering if there is a way to create a '.exe' file from ' .m' file in MATLAB, such that it can be run in machine which does not have MATLAB (like it can be done in C, C++).
I know writing a MATLAB function is one way, but I am not sure if it can run in machine without MATLAB.
Also I would like to hide my code and just create a script which can be run by a user using his own data files.
The Matlab Compiler is the standard way to do this. mcc is the command. The Matlab Runtime is required to run the programs; I'm not sure if it can be directly integrated with the executable or not.
If you have MATLAB Compiler installed, there's a GUI option for compiling. Try entering
deploytool
in the command line. Mathworks does a pretty good job documenting how to use it in this video tutorial: http://www.mathworks.com/products/demos/compiler/deploytool/index.html
Also, if you want to include user input such as choosing a file or directory, look into
uigetfile % or uigetdir if you need every file in a directory
for use in conjunction with
guide
Try:
mcc -m yourfile
Also see help mcc
If your code is more of a data analysis routine (vs. visualization / GUI), try GNU Octave. It's free and many of its functions are compatible with MATLAB. (Not 100% but maybe 99.5%.)
mcc -?
explains that the syntax to make *.exe (Standalone Application) with *.m is:
mcc -m <matlabFile.m>
For example:
mcc -m file.m
will create file.exe in the curent directory.
It used to be possible to compile Matlab to C with older versions of Matlab. Check out other tools that Matlab comes with.
Newest Matlab code can be exported as a Java's jar or a .Net Dll, etc. You can then write an executable against that library - it will be obfuscated by the way. The users will have to install a freely available Matlab Runtime.
Like others mentioned, mcc / mcc.exe is what you want to convert matlab code to C code.
The "StandAlone" method to compile .m file (or files) requires a set of Matlab published library (.dll) files on a target (non-Matlab) platform to allow execution of the compiler generated .exe.
Check MATLAB main site for their compiler products and their limitations.
I developed a non-matlab software for direct compilation of m-files (TMC Compiler). This is an open-source converter of m-files projects to C. The compiler produces the C code that may be linked with provided open-source run-time library to produce a stand-alone application. The library implements a set of build-in functions; the linear-algebra operations use LAPACK code. It is possible to expand the set of the build-in functions by custom implementation as described in the documentation.

How to tell Matlab to choose "plain-matlab-code" version of a function instead of mex version?

In my code, I have some compiled mex files. For compatibility reasons, I also have "plain" matlab code that returns the same things.
The idea is that if the user don't have the right version of the mex files, the code should still be able to run with the plain matlab version. It will be slower, but it should run.
The advantage is that matlab will automatically choose the plain matlab version if the right mex file is not present.
Now, my question si the following : is it possible to tell matlab to use, at some point, the "plain" version, even if the right mex file is present ? This is mostly for performances comparaisons.
Thanks a lot for your help,

Mex or Compile (mcc) Matlab function that uses toolkits

Environment:
Matlab R2012a (I have access to others if necessary)
All Toolboxes/Compiler installed
Ubuntu 12.04 64bit and/or Windows 7 64bit
I am working with the source for a software package written in Matlab (unfortunately its proprietary so no code examples...sorry), and one function briefly uses the Control System Toolbox and the Signal Processing Toolbox.
I have no problem running the code on my personal computer because I have every toolbox installed, however I would like to compile (mex or mcc) JUST the function using those two toolboxes. The goal, of course, is to run the software on a machine without those toolboxes, while leaving the remaining code open to change.
According to matlab, they have no problem with you compiling code that uses almost any toolbox. Here is the list of toolboxes that support mcc compilation:
http://www.mathworks.com/products/compiler/supported/compiler_support.html
The problem arises in that mcc no longer allows compiling with the -x option to create a mex-ed version of the function, so I'm forced to create a C executable (maybe? hopefully not). This particular function takes large matrices as parameters (impractical to write as a command line argument) and returns a structure of cell arrays.
The only way around this (as I see it now) would be to write the arguments (large matrices) to the hard drive in a binary .mat file , have the compiled C binary read in the arguments, run the algorithm, and finally save the return values in another .mat for the parent thread to load back into memory.
This seems totally impractical. I would greatly appreciate alternative suggestions. Please let me know if anything here in unclear. Thanks in advance!
[Edit 1] The codegen package does not support tf.m. It seems like this should be possible (and used to be possible with the mex -x option), but I'm at a loss. Any suggestions would be greatly appreciated!
I think the reason -x is not supported anymore is the fact that Matlab now has a product called "coder", which converts .m files to .c files and can also create .mex files from "suitable" .m files using the option -args to specify the input arguments: http://www.mathworks.com/videos/generating-c-code-from-matlab-code-68964.html

way to handle to write CUDA+MEX code in linux?

I try to write matlab mex code with Cuda integrated but it is just hard enough to compile and debug all around. Is there any better approach to code and test? I am on Matlab 2012b.
Currently I am writing code in sublime then compile it on matlab but I am also newbie at CUDA as well thus it is just hard to code it without seeing the result instantly.
The comment by Robert Crovella is interesting.
I just wanted to mention the way I was used to compile mex file with CUDA instructions (and which works also on different versions of MATLAB).
1) Compile by nvcc and transform the source code in C++ code by the command
system(sprintf('nvcc -I"%s/extern/include" -cuda "mex-fun.cu" -output-file "mexfun.cpp"', matlabroot));
2) Link it to Matlab by
mex -I/opt/cuda/include -L/opt/cuda/lib -lcudart mex-fun.cpp
This was originally suggested at the MATLAB Newsreader page.
I have both a matlab entry point (i.e. a file with the function "mexFunction") and a C++ entry point (a file with "main"), and the rest of my actual CUDA code is independant of what entry point was used.
This way, I can debug the code used for my MEX files using the standard set of CUDA tools (i.e. nvprof, cuda-memcheck, etc) without having to deal with the MEX file. Then once I'm sure I have no bugs or memory leaks, I just compile it to a MEX file. Alternately you can always just attach cuda-gdb to your MEX file, although your mileage may vary with this.