Modify the MaskDIsplay of a block by using c sfunction

4 views (last 30 days)
Hello everyone,
I'm wondering if it's possible to modify the MaskDisplay of a block by using a C sfunction. I know I can get the MaskDisplay parameter as string by using this Matlab instruction
get_param(gcb,'MaskDisplay')
but for my purpose ( ... and for the advisor of my internship -_-" ) I can't directly use it, because I have to stuck with C-Sfunction. so I'm here asking if if there is a C instruction to do that
thanks 4 reading any suggestion will be appreciate
have a good day
  1 Comment
grapevine
grapevine on 11 May 2012
Actually I have some parameters, which are passed to the sfunction, one of them is the number of inputs, another one is if I want to complement an input, and so on.
Based on these parameters the mask display change.
For a block with a fixed number of inputs I can do that by using a callback function.
But with a block where the inputs number is custom defined and modifiable, I am not capable to do that so I thought to cope this problem inside the mdlInitializeSizes method of the C-sFunction.

Sign in to comment.

Accepted Answer

Kaustubha Govind
Kaustubha Govind on 10 May 2012
Do you mean that you need to perform that command from the C-code? Is it because the MaskDisplay value is computed dynamically by the C-MEX S-function? If yes, in which callback method you would like to do this?
In principle, you can use mexCallMATLAB to call the set_param function from the C-MEX S-function, but you can set MaskDisplay only at "edit-time", ie, before mdlInitializeSizes is run, so the only place that you can do this might be mdlCheckParameters (although I haven't verified this).
  4 Comments
Kaustubha Govind
Kaustubha Govind on 11 May 2012
grapevine: Could you try looking at the examples linked off the doc page for mexCallMATLAB (which is linked to my answer). Since all the information that you need to change the icon is available from parameters, I think you should use the mdlProcessParameters callback - I think setting MaskType in mdlInitializeSizes will result in an error.
grapevine
grapevine on 11 May 2012
thanks a lot for your help
I'll keep u posted
have a good day

Sign in to comment.

More Answers (2)

Bruce Jackson
Bruce Jackson on 4 Sep 2012
Edited: Bruce Jackson on 4 Sep 2012
I have a similar question in that I would like to label the S-function mask based on the data from an external file I'm loading at mdlInitializeSizes. Is is possible to edit the mask from within a C++ Sfunction? Seems like there should be some way for Matlab/Simulink and an S-function to be able to communicate/interrogate each other beyond the specified S-function calls, for more flexibility.
However, (for instance), I can't seem to even use ssPrintf() inside of mdlProcessParameters or mdlCheckParameters; ssPrintf() works fine in mdlInitializeSizes, but using mexCallMATLAB() within mdlInitializeSizes doesn't appear to work.
  3 Comments
Bruce Jackson
Bruce Jackson on 5 Sep 2012
Edited: Bruce Jackson on 5 Sep 2012
I've defined a simple procedure in the C++ mex file::
void test(SimStruct *S) {
mxArray *lhs;
const char *cmd = "get_param(gcb,'name')";
mexCallMATLAB(1, &lhs, 0, NULL, cmd);
char msg[256];
if(lhs) {
int m = (int) mxGetM(lhs);
int n = (int) mxGetN(lhs);
sprintf(msg, "lhs = (%d x %d)\n", m, n);
ssPrintf(msg):
} else {
ssPrintf("lhs was null.\n");
}
}
If I call test(S) from mdlInitializeSizes() I get strange results: if cmd is set to get_param(gcb,'name') as shown, Matlab crashes. If I define cmd = "who" then I don't get the anticipated result (the contents of the workspace): returned lhs is a 2x1 cell array containing two strings, "MaskParam_L_1" and "filepath" (filepath is the sole parameter for this Sfunction) when I update the diagram (<ctrl>-d).
If I move the call to test() to either mdlCheckParameters() or mdlProcessParameters() nothing is printed in the command window, even after changing the parameter, updated the diagram, or running the model. Even a raw ssPrintf("hello\n") fails to print from inside either routine.
Note that I am not invoking mdlCheckParameters or mdlProcessParameters explicitly but I gather they are supposed to be called when I change a model parameter; but I can't see that they are being called even then.
Kaustubha Govind
Kaustubha Govind on 6 Sep 2012
Edited: Kaustubha Govind on 6 Sep 2012
The result of "who" is expected - essentially, your commands are running in the mask workspace of the S-function, which is why you are seeing mask parameters (MaskParam_L_1 could be internal data). Note that gcb could be empty (it returns the currently selected block in the model, not the currently executing block). mexCallMATLAB is a bad choice when errors may occur because I believe it terminates the MEX-function, you may want to use mexCallMATLABWithTrap to capture error information. It is safer to use the output of ssGetPath in place of gcb. Regarding mdlCheckParameters and mdlProcessParameters - after consulting the documentation, it seems like these functions are optionally called. Could you try setting a breakpoint instead of using ssPrintf (since stdout could be suppressed)?

Sign in to comment.


Bruce Jackson
Bruce Jackson on 7 Sep 2012
I can confirm that mdlCheckParams and mdlProcessParams are not being called, despite me changing the value of the single block parameter in the Simulink editor (in this case, the path to the file I want the SFunction to load data from) by using the debugger.
What I'm attempting to do is load an ANSI/AIAA S-119 model (see http://daveml.org ) into a Simulink Sfunction. That XML-based format provides nonlinear mappings between scalar inputs and outputs, so in addition to needing to change the number of input and output ports on initialization (the SFunction mdlInitializeSizes works very nicely to perform this function), I would like to be able to label the ports, too, and I fail to see a direct way to do this unless your suggestion of using ssGetPath (thanks!) lets me use the mexCallMatlabWithTrap method to issue mask drawing commands to my own block.
This indirect workaround seems cumbersome and unnecessary; I expected to find some introspection capability to allow a C/C++ SFunction be able to manipulate the appearance of the Simulink block that contains it.
Thanks very much for your support,
-- Bruce
  1 Comment
Kaustubha Govind
Kaustubha Govind on 7 Sep 2012
Bruce: At the risk of sounding trivial - have you place the statement "#define MDL_CHECK_PARAMETERS" above mdlCheckParameters and "#define MDL_PROCESS_PARAMETERS" above mdlProcessParameters?
Also, I'm confused about what your ideal/expected solution is - could you explain a little more. It appears that the solution I suggested is what you're saying is cumbersome?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!