Differences between the two Sample time settings in the MATLAB Function block in Simulink

I have a question regarding the MATLAB Function block in Simulink, specifically about the two Sample time settings shown in the attached screenshots (screenshots 1 and screenshots 2).
I would be extremely grateful if you could kindly explain the differences between these two settings and how they impact the behavior of the block. Furthermore, I am interested in understanding any recommended best practices or scenarios where one setting should be favored over the other. Could someone provide clarification on this matter?
For detailed reference, you can navigate to the screenshot interface by clicking on this link:
I truly appreciate your assistance and expertise in this matter.
Screenshot 1:
Screenshot 1
Screenshot 2:
Screenshot 2

 Accepted Answer

This is a really good question. I've never noticed it before. I'll provide my take on it. You could contact the Mathworks' tech support for an official answer.
First of all, a MATLAB Function block is actually a masked subsystem. Run get(gcbh,'Blocks') on a default MATLAB Function block returns the blocks below, which reflect the default input, output and other stuff.
>> get(gcbh,'Blocks')
ans =
5×1 cell array
{'u' }
{' Demux ' }
{' SFunction ' }
{' Terminator '}
{'y' }
So, Screenshot 1 approach, let's call it Block Dialog, can be brought up by right clicking the MATLAB Function block, selecting "Block Parameters (Subsystem)". It specifies the sample time of the masked subsystem. This sample time is available to be set only when "Treat as atomic unit" option is checked.
Screenshot 2 approach, there are actually two ways, Property Inspector or Model Explorer. They have the same effect. Changing the sample time in one way would automatically be reflected in the other way. This approach specifies the sample time of the ' SFunction ' block which let's assume is the "core" of the MATLAB Function block. Please also note that this sample time could also be grayed out when the "update method" is continuous.
So, overall, treat these two sample times as one is for the subsystem and the other is for a block inside the subsystem.
When everything is in default, or when both samle times are activated or apply
  1. Only need to specify one place, leave the other as -1
  2. You could specify both but they need to be the same. Otherwise, there would be an error.

4 Comments

Dear Fangjun Jiang,
Thank you very much for your prompt response and insights into my previous question. Your help is greatly appreciated.
To enhance the clarity of my inquiries and provide a more detailed context, I would like to share a Simulink model with you. This model will serve as an illustrative tool to demonstrate the specific scenarios and behaviors I am seeking clarification on. I have attached the Simulink model file to this comment as an attachment. For your convenience, I have included screenshots of the model in the body of this message, as shown below.
model
In this model, I have three main components: 1. the MATLAB Function block, 2. the Powergui, and 3. a MOSFET block. Through my simulations, I have observed that the discrete sample time settings in Powergui and the presence of the MOSFET block can influence the correctness of the output from the MATLAB Function block. My question pertains to how changes in parameters or the presence of other blocks within this model affect the output of the MATLAB Function block when none of its parameters have been altered. Specifically:
  • In scenarios where all three components are present, I have noticed that varying the discrete sample time settings in Powergui, as shown in screenshot 1, leads to different outputs from the MATLAB Function block. For example, when I set the Powergui discrete sample time to the following three sets of values, the MATLAB Function block's output results are distinct. Among these sets, the results for 5e-6 and 2.5e-6 align with my expectations, as per the MATLAB Function block's internal code, as shown in screenshot 2. However, the outcomes for the other two sets do not match my anticipated results, as depicted in screenshot 3. The three sets of sample times are as follows:
1) 5e-6, 2.5e-6
2) 1e-6, 2e-6, 3e-6, 4e-6
3) 6e-6
  • In the cases where the results do not align with my expectations (as mentioned in above), if I comment out the MOSFET block, the MATLAB Function block's output results become as expected.
My question is: How do changes in other blocks parameters (powergui) or the presence of other block (Mosfet) affect the output of the MATLAB Function block when none of its parameters have been altered?
I understand that my query may be somewhat complex. If there are any points that require further clarification, please feel free to ask in your response. Thank you once again for your valuable assistance.
Screenshot 1: Setting the discrete sample time in Powergui
Screenshot 2: The expected output results from the MATLAB Function block
Screenshot 3: The unexpected output results from the MATLAB Function block
What you described is not a surprise to me. It is caused by the sample time or execution rate of the blocks in the model. It could happen to all types of block and is not particularly related to only the MATLAB Function block.
To see this problem, in the model, at the "Debug" tab, Information Overlays, Sample Time, turn on Text and Timing Legend.
Because the model uses the variable step auto solver, the base execution rate is determined only by the PowerGUI block. Whatever the sample time the PowerGUI block uses, becomes the base execution rate of the model. You can change the sample time of the PowerGUI block using the numbers that you listed, and then press Ctrl+D to update the model, to see the sample time of the MATLAB Function block, y, is always the same.
Then inside the MATLAB Function block, you have a fixed value of 0.1 second and a simple discrete integration. This becomes problematic when the sample time is changing from 1e-6 to 6e-6.
There is a precision difference if you integrate with 1e-6 step, vs. 2e-6 step or 6e-6 step. 1e-6 is not exactly 1e-6, there is a precision loss when it is represented by a double type data. This precision loss is different for 2e-6 or 6e-6. When you count by 1e-6 or 2e-6 so many times to reach 0.1, there will be hit or miss and it is different when the step is 1e-6 or 2e-6. Also, you won't be able to hit 100 if you count by 3.
The solution to this is to use a discrete fixed step solver. For example, if you set the solver to be discrete with 1e-6 fixed step, then the result is always the same no matter what sample time you set for the PowerGUI block. The result may not be what you expected but they are consistent. You can't use 2.5e-6 for PowerGUI becuase it is not an integer multiple of the fixed step size (1.0E-6) specified for model
I don't think the Matlab Function block is a masked system. It looks like it's an ordinary subsystem
>> get_param(gcb,'Mask')
ans =
'off'
>> get_param(gcb,'BlockType')
ans =
'SubSystem'
It could be atomic or not depending on the check box in the block parameters.
If the Sample Time of the Matab Function block is -1 (inherited) then it will execute at the Sample Time of the Clock, which is 'continuous'. Signals with continuous sample time are evaluated at steps determined by the solver, which in turrn can be influenced by sample times of other blocks in the model if you're using a variable step solver.
@Paul, you are right. It is a "special" Subsystem block and not masked.

Sign in to comment.

More Answers (0)

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!