Modify the MaskDIsplay of a block by using c sfunction

4 次查看(过去 30 天)
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 个评论
grapevine
grapevine 2012-5-11
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.

请先登录,再进行评论。

采纳的回答

Kaustubha Govind
Kaustubha Govind 2012-5-10
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 个评论
Kaustubha Govind
Kaustubha Govind 2012-5-11
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 2012-5-11
thanks a lot for your help
I'll keep u posted
have a good day

请先登录,再进行评论。

更多回答(2 个)

Bruce Jackson
Bruce Jackson 2012-9-4
编辑:Bruce Jackson 2012-9-4
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 个评论
Bruce Jackson
Bruce Jackson 2012-9-5
编辑:Bruce Jackson 2012-9-5
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 2012-9-6
编辑:Kaustubha Govind 2012-9-6
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)?

请先登录,再进行评论。


Bruce Jackson
Bruce Jackson 2012-9-7
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 个评论
Kaustubha Govind
Kaustubha Govind 2012-9-7
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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by