Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

matlab comand as simulink blocks

227 views
Skip to first unread message

Sangeetha Nair

unread,
Mar 13, 2009, 7:33:01 AM3/13/09
to
Dear all
I would like to know how to convert the matlab commands like CARE(continuous algebraic riccati equation) ,hinfisys etc to simulink blocks . I have tried with embedded function blocks . But the following errors are seen.
Pgm written in embedded function block
function [control1,control2] = cntroller(x1,x2,x3)
X=[x1;x2;x3];
A=[x1 1 2*x1;x2 sin(x2) x3;x3 x2 x1];
B=[0 0;1 0;0 1];
Q=[1 0 0;0 1 0;0 0 1];
[X,L,G]=care(A,B,Q);
U=-G*X;
control1=U(1,1);
control2=U(2,1);

errors seen
1.Expected either a logical, char, single, or double.
Found an mxArray. mxArrays are returned from calls to the MATLAB interpreter and are not supported inside expressions. They may be used on the right-hand side of assignments and as arguments to MATLAB calls.
Function 'Embedded MATLAB Function' (#18.268.269), line 9, column 4:
"G"
2.Undefined function or variable 'U'.
The first assignment to a local variable determines its class. Inputs, outputs, and parameters inherit their class from the model explorer.
Function 'Embedded MATLAB Function' (#18.282.283), line 10, column 10:
"U"
3.Errors occurred during parsing of Embedded MATLAB function 'Embedded
MATLAB Function'(#18)
4.Undefined function or variable 'U'.
The first assignment to a local variable determines its class. Inputs, outputs, and parameters inherit their class from the model explorer.
Function 'Embedded MATLAB Function' (#18.299.300), line 11, column 10:
"U"


Also embedded function block supports only a few matlab functions.I need to use it with real time workshop to run the real time application and hence c code must also be subsequently generated from this simulink block.
Hope some one has encountered a similar problem and would kindly respond.

Thank u

Phil Goddard

unread,
Mar 13, 2009, 12:46:01 PM3/13/09
to
"Sangeetha Nair" <sangee...@gmail.com> wrote in message <gpdg9d$l62$1...@fred.mathworks.com>...

To get your code to work in simulation you need to have a look at the doc for the embedded MATLAB function block; inparticular what the code needs to look like for you to call functions not intinsically supported by the eml block.

See
>> doc eml.extrinsic
for the specifics.

To get it to work with Real-Time Workshop you will need to find a C-code equivalent of the functions not intrinsicly supported by the eml block.
The MathWorks does not supply such routines.

Phil.

Sangeetha Nair

unread,
Mar 13, 2009, 11:49:01 PM3/13/09
to
Thank u, Phil..could u please tell me where to search for 'doc eml.extrinsic'?Also if i need to write the c-code, could I use S-functions?If so...inline or noninline or wrapper s function must be used? kindly assist.

thank u


"Phil Goddard" <philgodd...@telus.net> wrote in message <gpe2k9$la4$1...@fred.mathworks.com>...

Phil Goddard

unread,
Mar 15, 2009, 2:29:02 PM3/15/09
to
"Sangeetha Nair" <sangee...@gmail.com> wrote in message <gpf9fd$qqu$1...@fred.mathworks.com>...

> Thank u, Phil..could u please tell me where to search for 'doc eml.extrinsic'?Also if i need to write the c-code, could I use S-functions?If so...inline or noninline or wrapper s function must be used? kindly assist.
>
> thank u
>

At the MATLAB command line type
>> doc eml.extrinsic

or bring up the documentaton by typing
>> doc
and then in the search box at the top left search for the terms eml and extrinsic.

With the S-function it depends if you want to use the C-code as part of the simulation or just for (RTW) code generation.
If you want to use MATLAB's code during simulation and then your c-code during code generation then you could write an m-code S-Function to call the MATLA code and a TLC file which is used during code gen.
If you want to use the c-code during simulation then you'll want to write a c-code S-function, then if you also write a TLC file the code can be inlined, but if you don't wite a TLC file your code will be non-inlined.
Which is best is for you to determine given your real-time processor constraints.

You'll need to spend time reading much of the S-Function and RTW doc to decide which is best for you and how to go about implementing it.

Phill.

Sangeetha Nair

unread,
Mar 16, 2009, 12:30:04 AM3/16/09
to
Dear Phil

Thank u so much.Yes, i think am on the right track now reading more on s functions and RTW.
I read some where down the line in the documnetation provided that m-files (level 1 mfiles)are not suitable for real time implementation and we need to resort to C-Mex files.So now efforts are on to write a suitable C-Mex file for the same. Hope its in the right direction.
Thank u


"Phil Goddard" <philgodd...@telus.net> wrote in message <gpjhde$9or$1...@fred.mathworks.com>...

Sangeetha Nair

unread,
Mar 31, 2009, 1:44:02 PM3/31/09
to
Dear Phil,

As suggetsed by you and keeping in mind my requirements, i have written a C-mex S function. Thank you.
Now I am faced with yet another difficulty. As I try to enter the name of my C-mex S function into the S function block (of simulink) and do simulation, an error appears that such a file does not exist. Shuould I do somethng more to link my Cmex S function with simulink so that it recognizes my S function? I tried to give the "mex file name" command at the command window...but again an error comes as ...
"C:\MATLAB701\BIN\WIN32\MEX.PL: Error: 'sdrecode.c' not found.
??? Error using ==> mex
Unable to complete successfully"
Hope to get a reply from you...
Thank you
Sangeetha

"Sangeetha Nair" <sangee...@gmail.com> wrote in message <gpkkkc$la3$1...@fred.mathworks.com>...

Phil Goddard

unread,
Mar 31, 2009, 4:48:01 PM3/31/09
to
> Dear Phil,
>
> As suggetsed by you and keeping in mind my requirements, i have written a C-mex S function. Thank you.
> Now I am faced with yet another difficulty. As I try to enter the name of my C-mex S function into the S function block (of simulink) and do simulation, an error appears that such a file does not exist. Shuould I do somethng more to link my Cmex S function with simulink so that it recognizes my S function? I tried to give the "mex file name" command at the command window...but again an error comes as ...
> "C:\MATLAB701\BIN\WIN32\MEX.PL: Error: 'sdrecode.c' not found.
> ??? Error using ==> mex
> Unable to complete successfully"
> Hope to get a reply from you...
> Thank you
> Sangeetha

What is the exact command you typed?
Are you sure you have a file called sdrecode.c on your MATLAB path?

Phil.

Sangeetha Nair

unread,
Mar 31, 2009, 9:10:18 PM3/31/09
to
"Phil Goddard" <philgodd...@telus.net> wrote in message <gqtvi1$pbo$1...@fred.mathworks.com>...

Dear Phil

sdrecode is the name of my C mex s function(the flile I created).so to link it , I typed,
mex sdrecode.c, on which the above mentioned error came.
You have mentioned of a 'MATLAB path'.shud i be saving my new created file sdrecode into some such location. If so, wat could be this path?

(Only if I use mex to generate an executable, will my user generated C-Mex file(sdrecode) shall be recognised by Simulink, is it not? )

Thank You
Sangeetha

Phil Goddard

unread,
Mar 31, 2009, 11:32:01 PM3/31/09
to
"Sangeetha Nair" <sangee...@gmail.com> wrote in message <gquetq$caf$1...@fred.mathworks.com>...

Yes, you need to mex the code (which generates the dll that is used by Simulink).
So you need to
>> mex sdrecode.c

The path is a fundamental aspect of MATLAB and you should look at the doc about it.
For your purposes assume MATLAB can only execute files that live in a directory that is on the MATLAB path.
To see the MATLAB path type
>> pathtool
which will bring up a UI showing a long list of directories.
sdrecode.c needs to be in one of these directores.

I would recommend that you have sdrecode.c in the same directory as your Simulink model.
If you can open your model then that directory is on the path and sdrecode.c should be mex-able.

If sdrecode.c is in the same directory as your Simulink model, and you can open your Simulink model, and you still get the above error when you try mexing then I can only assume that you have made a typo with respect to the file name.

Phil.

Sangeetha Nair

unread,
Apr 1, 2009, 2:18:02 AM4/1/09
to
Thank You Phil....
I tried as per your directions (inlcuded the model and the s functions in the same directory which falls in the MATLAB path)but still the same error command pops up.I havnt made any typo errors too....i chckd. one additional info i hav gt nw...timestwo is an example C-mex function supplied in MATLAB documentation. It runs in simulation(implies ,its corresponding .dll is already present),but when 'mex timestwo.c' is given at the command line, the same error pops up...
C:\MATLAB701\BIN\WIN32\MEX.PL: Error: 'timestwo.c' not found.
??? Error using ==> mex
Unable to complete successfully"
Does it mean there is a problem with the mex command usage? I am working on licenced version R2007a.
Hopefully, I have been able to supply necessry details...so grateful to you for ur replies and sorry to pester you too much ....its vey urgent n am unable to get any help frm nywere else...
thank u
Sangeetha


"Phil Goddard" <philgodd...@telus.net> wrote in message <gqun7h$efg$1...@fred.mathworks.com>...

Sangeetha Nair

unread,
Apr 2, 2009, 10:24:01 AM4/2/09
to

Dear Phil

Thank you for the instructions...there indeed was probelm with the path. The file was in the user profile directory and needed to be brought to the matlab path using the cd command....Now it is solved.
Thank you
Sangeetha
"Sangeetha Nair" <sangee...@gmail.com> wrote in message <gqv0uq$8rt$1...@fred.mathworks.com>...

Sangeetha Nair

unread,
Apr 10, 2009, 1:41:01 AM4/10/09
to
"Phil Goddard" <philgodd...@telus.net> wrote in message <gqun7h$efg$1...@fred.mathworks.com>...

Dear Phil
Thank you for your help so far which has brought me a lot forward in writng S-functions for my use, which i have been able to successfully compile and run in simulink. There is a slight problem when it comes to the real time implementation. I use math.h file in my s function. In MATLAB help, it is mentioned that "rtwintgt.h" file must be included after the math.h file , else ,though the S-function may run in simulink, it may not build in Real time workshop. However when I include the rtwintgt.h file, the mex operation is not done...the following error appears..
lcc preprocessor error: sdrecode.c:14 Could not find include file "rtwintgt.h"
Warning sdrecode.c: C:\MATLAB701\simulink\include\simulink.c: 2671 assignment of pointer to struct SimStruct_tag to pointer to double
Warning sdrecode.c: C:\MATLAB701\simulink\include\simulink.c: 2671 assignment of pointer to double to pointer to struct SimStruct_tag
1 errors, 2 warnings
C:\MATLAB701\BIN\WIN32\MEX.PL: Error: Compile of 'sdrecode.c' failed.

??? Error using ==> mex

Also, when I try to build the model which contains the S-function, without the rtwintgt.h file, the following error in generating TLC file appears
### Loading TLC function libraries

.....
### Initial pass through model to cache user defined code
Error: File: C:\Program Files\MATLAB\R2006a\toolbox\rtw\targets\rtwin\rtwin\rtwinlib.tlc Line: 28 Column: 16
Undefined identifier RTWinBoards
Error: File: C:\Program Files\MATLAB\R2006a\toolbox\rtw\targets\rtwin\rtwin\rtwinlib.tlc Line: 28 Column: 28
The left-hand side of a . operator must be a valid scope identifier
Error: File: C:\Program Files\MATLAB\R2006a\toolbox\rtw\targets\rtwin\rtwin\rtwinlib.tlc Line: 43 Column: 25
Undefined identifier RTWinBoards
Error: File: C:\Program Files\MATLAB\R2006a\toolbox\rtw\targets\rtwin\rtwin\rtwinlib.tlc Line: 43 Column: 39
The + operator only works on numeric arguments
Kindly help.
Thank u

Sangeetha Nair

unread,
Apr 10, 2009, 2:40:05 AM4/10/09
to
"Sangeetha Nair" <sangee...@gmail.com> wrote in message <grmm5d$hsk$1...@fred.mathworks.com>...

This is the error which comes when grt.tlc is used as the system target file in the config parameters box. yet another error comes for , when the system target fiel used is rtwin.tlc
> Kindly help.
> Thank u

Esmail Alandoli

unread,
Nov 6, 2016, 11:42:17 PM11/6/16
to
"Sangeetha Nair" wrote in message <gqv0uq$8rt$1...@fred.mathworks.com>...
0 new messages