Simulink - vary which part of a structure is accessed in a matlab function block (using a bus)

Hello,
Within a matlab function block can I dictate what part of a structure is accessed based on one of the function blocks inputs?
I am using an embedded 'matlab function' block in simulink to control the value of a force. The force depends on time and 1 other parameter (that I have called 'which_force'). The various values for Force are contained in a structure. I use a bus to grant Simulink access to the structure.
The structure looks like:
Forces.which_force1.Heave
Forces.which_force2.Heave
Here is a simplified version of the code:
function F_Heave = get_force(Forces, Clock, which_force)
F_Heave = Forces.(which_force).Heave(Clock)
An error occurs with (which_force) that states:
'Non-constant expression or empty matrix. This expression must be constant because its value determines the size or class of some expression. In this context, the value of the string must be known.'
I have also applied which_force from a constant block and I still receive the same error message.

 Accepted Answer

I think the only way this code might work is if the variable which_force is configured as a non-tunable parameter of the block, instead of as an input signal.

5 Comments

From the mathworks documentation: "You cannot change the value (of the Global Nontunable Parameter) during simulation"
Unfortunately which_force is calculated based on the output of the previous time step of the function block; and so its value will have to change.
Is there any other way around this?
Cheers, Leo
Do you need to be able to generate code from the model? If not, perhaps you can separate out this part of your code into another function and call it by declaring it as a coder.extrinsic? The MATLAB Function block doesn't look inside extrinsic functions, so you shouldn't see the error in question.
You could try to use some sort of controlflow to choose which struct is used instead of dynamic field names.
IE
switch(which_force)
case 'which_force1'
F_Heave = Forces.which_force1.Heave(Clock);
otherwise
F_Heave = Forces.which_force2.Heave(Clock);
end
or if 'which_force' is numeric
if(which_force == 0)
F_Heave = Forces.which_force1.Heave(Clock)
else
F_Heave = Forces.which_force2.Heave(Clock)
end
Kaustubha Govind:
The coder.extrinsic worked and seemed like a neat solution. However the model run took 18 minuts as opposed to the current method (using simulink switch blocks) which takes 50 seconds.
Ketan :
The controlflow that you suggested brought the simulation time down to 150 seconds but was still not quick enough to compete with the switch blocks which take 50 secs.
The reason I did not wish to continue using switch blocks is that I am expanding the model and they will take a long time to create as which_force has 29 options and there are many variables (thus switch blocks) that use depend on it. Unfortunately it looks like I will need to continue with the switch blocks.
Thanks for both your help,
Leo
Hi Leo,
If you are evaluating the performance of a model containing MATLAB Function blocks, you may want to disable debugging for the MATLAB Function blocks. By default they are configured for debugging. See the following page for information on controlling function block debugging:
Ketan

Sign in to comment.

More Answers (0)

Categories

Asked:

Leo
on 9 Oct 2013

Commented:

on 20 Oct 2013

Community Treasure Hunt

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

Start Hunting!