Error Handling WITHOUT using try-catch statments

Hello,
I am trying to convert some of my MATLAB functions in to a form that is compatible with Embedded MATLAB.
I have a try-catch statement that attempts to run a simulation (try) and if the simulation fails (catch) returns a zero. By doing this the function can deal with failed simulations without crashing.
Unfortunately embedded MATLAB does not allow try-catch statements.
Does anyone know of a way I can handles this error without using try-catch?
Thanks.

2 comentarios

Kaustubha Govind
Kaustubha Govind el 19 de Mayo de 2011
Do you mean that you are simulating another model from within an Embedded MATLAB Block in a Simulink model? Is there a reason you chose to use the Embedded MATLAB block as opposed to the Model reference block?
One potential solution would be to perform the try-catch in a separate MATLAB function, which is called from the Embedded MATLAB block after declaring it using eml.extrinsic (coder.extrinsic starting in R2011a).
Will
Will el 19 de Mayo de 2011
No I am writing an m-file which conforms to the Embedded MATLAB rules - then I am going to convert it to C-code. It will not be put inside a Simulink model.
I also know you cannot use the MATLAB 'sim' command so I am having to write my own C interface to replace the aspects of the m-file which use the sim command.

Iniciar sesión para comentar.

Respuestas (2)

Arnaud Miege
Arnaud Miege el 19 de Mayo de 2011

0 votos

I wouldn't recommend trying to run a Simulink model within an Embedded MATLAB Function, which itself resides in another Simulink model. It means that at each time step of the "caller" model, you're running a simulation of the "called" model. This is extremely inefficient as you could potentially have thousands of time steps. As suggested, a better solution would be to use a Model Reference to include the "called" model by reference into the "caller" model.

5 comentarios

Will
Will el 19 de Mayo de 2011
I am not using a Simulink model within an Embedded MATLAB Function - I am simple writing an m-file which I intend to convert to C code.
The m-file calls the model and the model returns results which are then processed in the m-file.
I probably provided too much information: all I wanted to determine is if there is another way to handle errors (other than a try-catch statment)?
Arnaud Miege
Arnaud Miege el 19 de Mayo de 2011
That won't work. You can't generate C code from the sim command. These are the functions supported for code generation:
http://www.mathworks.com/help/releases/R2011a/toolbox/eml/ug/bq1h2z7-11.html
Using Simulink Coder, you can generate C code from the model itself.
Will
Will el 20 de Mayo de 2011
Hi Arnaud, thanks for your feedback.
I have already created C code for the Simulink model, imported into Visual Studio and verified it is running correctly. I know I can't use the 'sim' hence I am writing an interface for the model code in C.
Regarding the error handling, perhaps another question might lead us to a suitable answer:
If I provide a model in Simulink with a set of model parameters that cause the simulation to reach NaN at time x, the simualtion will stop and MATLAB will generate an error. Now, if I generate code for this model, provide it with the same parameters and run it in the generated C - what will happen? Surely somewhere - a flag is raised or a return statement is null or something is returned from the model_step function that I can use to determine that the simulation has failed?
Thanks.
Arnaud Miege
Arnaud Miege el 20 de Mayo de 2011
I'm not sure, sorry. One thing is with the generated C code, you only work one time step at a time, there's no notion of simulation time, so I'm not sure it makes sense to think of a simulation having failed.
Will
Will el 23 de Mayo de 2011
Ok. Well I will have a look through the generated code to see how/if it deals with NaNs at a given time step - if I can catch the NaNs in the generated code then essentially I can determine whether the simulation has failed.

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 19 de Mayo de 2011

0 votos

The only error control mechanism supported by C is signals. I don't know if error conditions produce signals; I do not recall having heard anyone refer to that as a possibility.

Categorías

Más información sobre Simulink Environment Customization en Centro de ayuda y File Exchange.

Preguntada:

el 19 de Mayo de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by