I am trying to call clear all from a simulink Level 1 S-function. I've take a look over doc mexCallMATLAB but I do not know how to use it. I've wanted to clear the work space after my function is called.
Are you sure that you're writing a level-1 S-Function? If you are, then you shouldn't. These days both m-code and c-code S-Functions should all be written as level-2.
You haven't specifically indicated whether you are writing in m-code or c-code, but since you've mentioned mexCallMATLAB it's assumed that you are using c-code, in which case you should use
mexEvalString("clear('all');");
If you're using m-code, then just use
evalin('base','clear all');
Related
I want to exclude the Simulink Function block from code generation. This means the Simulink Function blocks are in place during simulation (to keep the DiagnosticMonitorCaller block from the AUTOSAR library happy) but are excluded when Matlab is compiling the model for code generation. The reason for this is that the functions are already there and not generated from Matlab/Simulink.
The work around is to 'comment out' during compiling and building but this is not really user and subversion friendly.
Can I use another block to 'fake' the function in place during simulation or do some settings on the Simulink Function block I didn't find?
This is achieved using the Environment Controller block.
For my coursework project in MATLAB, I have decided to build a drive-line model within Simulink, using the SimDriveline toolbox. The idea is to get the user to input values for the various parameters that are associated with each part of the model, such as the engine or the transmission. I would like to able to write this in a MATLAB script, but I'm not sure how to assign the values that are input to the Simulink model. For instance, the stock sdl_vehicle example that comes with SimDriveline. I am aware of the sim() command, but I am still confused on how to use it properly.
Also at the end of the simulation, the program is supposed to display the graphs that are collected in the scope window. I know that in the window itself that the scope can be printed to a figure, but is it possible to print that scope to a figure through MATLAB script?
This is the first time I have ever used a program like MATLAB. I would appreciate any help I could get, many thanks in advance!
There is a simulink block called simin:
http://de.mathworks.com/help/simulink/slref/fromworkspace.html?searchHighlight=simin
I used it some days back and it worked quite well. You can use the block in your model and define some signals/varibles as input.
After that you may write a Matlab-Script with an input function to set all the previous defined input values.
I'm using a Matlab level 1 S-function several times in a model but don't want the mutual overwriting of global/persistent variables.
A solution could be work vectors but there is little documentation about level 1 S-function work vectors. Where do i get the SimStruct S for the ssSetNumRWork function?
Thanks!
The right thing to do is upgrade the code to be a level-2 S-Function, which shouldn't be difficult.
Level 1 m-code S-functions don't have work vectors (and all of the ssGet/Set functions are for c-code S-Functions anyway). A reason they aren't recommended any more is that they have limited functionality -- there's no nice way of getting around that without using Level 2 functionality.
You can use persistent variables (within each subfunction of a level-1 S-Function). But the point of global variables to enable them to be seen everywhere, so trying to use them without wanting everyone to see them seems pointless. (Note: you should never use global variables anyway.)
Having said that, if you really want to use Level-1 functionality, then within each S-Function you can use the getappdata and setappdata functions to act like work vector storage. But if you're going to go to that trouble, upgrading to a level-2 S-function will be easier anyway.
Matlab Coder is a recently released MathWorks product. My understanding is that it is a Matlab-to-C compiler with the biggest advantage over previous solutions being that the resulting program does not need to be linked against a Matlab shared library.
Can someone with access to this product confirm the above? What are the dependencies of the translated programs and what kind of performance are we talking about? Also I would really like to see some example outputs, to know if the resulting C programs can be understood and improved without access to the Matlab source.
If done right this could be very powerful, allowing rapid prototyping in Matlab and instantaneous conversion to C when things are getting serious. I kind of whish it doesn't work well so that Python+Numpy+Scipy.weave is still superior ^^.
MATLAB Coder can allocate memory using malloc, so you can generate C code from MATLAB functions that operate on dynamically sized data. You can also choose the option of static allocation with a maximum size for variables.
RE: using BLAS for matrix multiplication – while the generated C code doesn’t automatically include any processor/platform specific optimizations, there is a feature called Target Function Library, which that allows users to write their own implementation of primitive operations (such as matrix multiplication), and include them in the generated code. You can hook up BLAS libraries to MATLAB Coder via that method. There’s also an ability to include optimized processor specific calls for larger functions through custom code integration and conditional compilation that lets you specify one set of code to use for code generation, and another set for simulation (for example, an optimized FIR function for an Texas Instruments DSP and functionally equivalent code for simulation that can execute on your PC written in C or in MATLAB).
Hope this is helpful --
Arvind Ananthan; Product Manager for MATLAB Coder; MathWorks
I am using Matlab Coder with Real Time Workshop (RTW) in order to generate self standind standard C code.
First of all you are asked to use a Matlab subset called "Embedded Matlab" you can find the doc about it on the web
You also have to avoid any dynamic exploitation of variables and you can't obviously generate c code for plots or figures.
The code it generates could be a mess to understand but it works.
you should actually not try to understand it. In a certain way it is as you would try to understand the assembler your compiler generates from a C code you wrote, quite pointless.
another thing you should take care of is to declare persistent big data types (vectors, big arryes, etc.) otherwise they will be allocated into your stack...
good luck!
MATLAB is a pass by value language. I have a recursive function that processes pixel's neighbors. It is very expensive to make the copy of the image (in my case two images) each time the function is called.
I used global variables to solve the problem. Is there any other way to make a recursive function modify an array?
You have three options here, but maybe you don't need any of them, since Matlab used 'copy-on-write', i.e. variables are only copied if you modify them.
As #gnovice mentions, you can use a nested function. Variables used inside the nested function are shared between the nested function and the enclosing function. Nested functions are somewhat tricky to debug, and a bit more complicated to write/understand.
You can store your images as properties of a handle object, which is passed by reference.
You can write code differently in order to not use a recursive function, since Matlab isn't the best language for using those. If you have access to the image processing toolbox, you may be able to use functions like blockproc, or im2col to rewrite the function.
Finally, if you want to stay with your current scheme, I strongly suggest using persistent variables instead of globals.
MATLAB is not always pass-by-value, newer versions of MATLAB do pass-by-reference under some circumstances, see in-place operations and a more general discussion about MATLAB memory management in this SO post.
Without tail-call optimization it is inefficient to use recursion and MATLAB does not have it as far I know, but every recursion can be transformed into a loop.
If you make your recursive function a nested function within another function where the image data is stored, then the recursive function can modify the image data without needing to have it passed to it.
This is a common misconception. Although the sytanx of MATLAB is pass by value, it does not actually pass by value as in C. The interpreter is smart enough to only make copies when necessary. So you should just go ahead and pass by value and see if you run into memory problems.
As other posters have noted, you should try to avoid recursion in MATLAB anyway.