How to run C code after automatic C code generation with Matlab? - c

I wrote a script in MATLAB and I am trying to generate C code.
I have not used C over the years so I am a little bit confused rn. I am using the MATLAB coder to generate C code automatically. This worked perfectly and now I am trying to open the C code in the Visual Studios environment and execute it. MATLAB coder did generate a C file called main.c. I guess it is an example of how to use the generated function appropriately. However, I am not able to run this program because I could not find the run button. Does anyone know, where the button is hidden?
Thank you in advance!
enter image description here

This sounds like what you need.

Related

Matlab crashes with S-function after mdlOutputs

I'm doing my "main" S-function based on the Matlab template with mdlStart mdlOutputs, etc, which communicates with some Ansi C files that I had, and this S-Function is used in Simulink. I compiled the files correctly with mex and in order to debug I'm using Visual Studio 2015. I can set and use breakpoints so everything is working fine.
The problem is, after finishing 'mdlOutputs' function, where all the contents are correctly printed into Matlab the debugging goes into a breakpoint and it goes into 'simulink.c'. After that, the Debug is broken saying that "libmex.pdb" cannot be found.
If I run the Simulink model without Visual Studio in the loop, Matlab just crashes and stops working.
So, any idea on how to check properly where it is giving me an error? Also, do you have any clue on why the process crashes after leaving mdlOutputs and before entering mdlTerminate? What happens in-between these two functions?
I'm using Windows 7, 64-bit and Matlab 2012b (I'm going to try and run it in the 2015b).
I think that I finally found out the solution (even though I quite don't understand it yet).
In the mdlInitializeSizes(SimStruct *S) I substituted ssSetOptions(S, 0); by ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE); like it is suggested here.

Converting Matlab/C function into version without sourcecode

Im totally new to Matlab. How can I convert a simple Matlab/C function INTO a version that can be run in Matlab WITHOUT showing the source code?? Please help!
As per your clarification the answer you're looking for is the often forgotten matlab pcode.
pcode is a great tool which allows you to distribute matlab code, without giving up the secrets of your source code (m-file). pcode files can ONLY be created on functions. So if you wrote a simple function:
function [y] = myfunction(x)
y=x.^2;
end
Then you could create a pcode file from this with the matlab command :
pcode('myfunction');
you would then have a myfunction.m and a protected myfunction.p.
There is a plethora of online documentation of people trying (unsuccesfully) to translate pcode back to an m-file.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/272505
http://www.mathworks.com/matlabcentral/answers/9848-how-to-decrypt-a-pcode
http://www.mathworks.com/matlabcentral/answers/75012-pcode-obfuscation-cracked-alternatives
As for compiling your C-code, I am not an expert in that area but there are many tools to do so. I personally tend to use gcc. Any further questions about compiling C-code should probably be opened as a new question, or search for some tutorials on getting started with C.

Can I use C and ActionScript together?

I'm doing a project where I need to create some kind of GUI for the user as well as control some servo motors.
I'm thinking of using ActionScript for the GUI and C for to control the hardware. Is this even possible? How do I make ActionScipt talk to C and vice-versa?
Obviously the C part will be driving the motors and will send the data to the ActionScript GUI
to be displayed to the user.
Is the above possible? If yes, could someone kindly provide me some pointers?
Thank you very much!
Edit: How do I pass a variable (e.g. x = 5) from ActionScript to be printed in C? Or is this not possible?
"You could go via ASP to a DLL as TG suggested or you could probably call an ANSI C (command line) app and pass in variables (at least on a Windows PC)... that owuld be done using FSCommands. see this tutorial
http://www.actionscripts.org/tutoria...es/index.shtml"
Originally replied by
Jesse Stratford
ActionScript.org Cofounder
This is taken from here
I don`t know the specifics of programming servo motors but basically, if you want some C to AS3 interaction you could do one of the following:
Compile your C/C++ code using flascc.
Wrap your C code in a native extension (if you are targeting AIR).
As sberry suggested - use Sockets.

Something wrong with C wrapper for Matlab

I'm currently facing an apparently silly problem with Matlab but I just can't figure out what is wrong (or better yet, I think it is wrong, but I'm being told it should work).
I have a Matlab script, "myscript.m" which needs to use a C-programmed function, "myfunc.c" which in turn has been compiled (or "lives") inside a library, "mylib.dll". In order for "myscript.m" to access and execute "myfunc.c" there is also a "myfunc.m" Matlab script, which is being called in "myscript.m". However, "myfunc.m" is a completely empty file, except for some comments (in no particular format or pattern either). All of these files were given to me and I'm being told that as it is, Matlab should correctly execute "myfunc.c" because "myfunc.m" is acting as a wrapper.
The problem I'm having is that it is of course not working. When I execute the line in "myscript.m" that reads:
output1 = myfunc(input1, input2);
I get Matlab errors saying that I'm trying to execute a script as a function. In my mind, this is correct, because I'm sending input and requesting outputs to something that is stated to have neither, since "myfunc.m" is empty (except for the comments, which are NOT code).
I think that Matlab has no way (as it is) to know that I want to access the C code inside "mylib.dll". I also DO NOT have a header file "mylib.h" so that I can load the library in Matlab using loadlibrary.
My question is, given the current description, could this execution work at all? Is there any way to make it work, or something that I'm missing? One possibility is that this is an old wrapper format and it currently doesn't work anymore.
If anyone knows anything or has suggestions, they will be greatly appreciated. Thanks!!!
Hugo
it sounds like myfunc.c is a mex file. try compiling it in Matlab using mex command. The dll extension is maybe old version matlab.
Have you tried with loadlibrary? You need a .h file but it's easy recreate on if you know the prototype of the function.

Call MATLAB function from a C program [duplicate]

I have some code that plots triangles in MATLAB.
I need to be able to somehow execute this code from my C program which generates these points.
Is that possible? How can it be done?
Just a thought:
Can I somehow embed MATLAB code in C, so that it can compile on a C compiler?
The Mathworks site has full details; a demo video of calling the Matlab engine from C, and also the Matlab to C Compiler.
As mentioned previously by answerers, you can call a live copy of MATLAB from C via the MATLAB Engine interface.
If the end-product needs to be used where there is no live copy of MATLAB, you can deploy the application using MATLAB Compiler. However, MATLAB Compiler does not, as another answer has suggested, convert MATLAB programs into C code (and hasn't done for a few versions now). Instead, it archives and encrypts your MATLAB program, and packages it into an executable or shared library that executes against the MATLAB Compiler Runtime (shareable royalty-free). The executable or shared library can then be called from C.
Alternatively you could go the other way around, and call your C code from MATLAB, using either loadlibrary or MATLAB's MEX interface.
Update: As of release R2011a, you can also use MATLAB Coder to generate C code directly from a subset of the MATLAB language.
Look at this presentation about integrating MATLAB Algorithms in C or C++ Applications http://www.mathworks.com/videos/integrating-matlab-algorithms-in-c-or-c-applications-86431.html

Resources