Get sample rate in a Matlab function block in Simulink

If i use MATLAB function block in a Simulink project, is there a way of determining the sample rate in the code in that block? If I use a MATLAB System block, I can use getSampleTime(obj).SampleTime to give me the time for a frame and then use the frame size to give me the time for a sample, but that won't work for a MATLAB function block. I'd like to use the MATLAB function block if possible because the coding is simple. Is there a way to get the sample rate or am I stuck using a MATLAB system block?

4 Comments

Paul
Paul on 24 Dec 2021
Edited: Paul on 24 Dec 2021
I'm not familiar with the Matlab System block and so am not sure how to translate that functionality to the Matlab Function block. In the Function block, Ports and Data Manager what is the Update Mehod and Sample Time?
I'm not sure what you mean about the Update Method. The signal is audio, so the sample rate would be something like 48kHz or 44.1kHz. The sample time would be the reciprocal of that. In a MATLAB System block, my code can determine the sample rate for a frame by using the getSampleTime(obj).SampleTime method. Is there any way that my code in a MATLAB Function block can similarly determine the same thing?
Like any other, or at least most?, blocks in Simulink the Matlab Function block has a sample time. It's either Inherited, or Discrete, or Continuous. See this doc page for how to select it. But I'm not sure that's what you're really asking about. Can you clarify what you're actually trying to do with the Matlab Function block?
Yes, the sample time is inherited from the audio signal in this case. I'm just trying to determine what that sample time is from my MATLAB function code. It seems there may not be a way of doing this. The suggestion from jibrahim may be the only way that I can do this.

Sign in to comment.

Answers (2)

Hi Glenn,
I've accomplished this in the past using a probe block:
That block can output a vector representing the sample time. You should be able to feed this value to your MATLAB function block (as an input) and use the value.

2 Comments

I would prefer not to have another block for the sake of simplicity, but if there's no way of determining the sample time internal to a function block, it sounds like I either need to feed the sample time as an input or use a MATLAB System block.
It would be nice to be able to code the equivalent of a Probe block inside the MATLAB Function block. Can you add that feature for the next version of Simulink? :)
You might be able to accomplish this by calling simulink.block.getSampleTimes inside the MATLAB function block:
However, that function does not generate code, so you will have to use it in conjunction with coder.extrinsic and coder.const. I think using a probe block is much more straightforward.

Sign in to comment.

Hi Glenn,
You could also use the following workaround to access the sample rate inside the MATLAB function block:
coder.extrinsic('get_param');
sampleTime=str2double(get_param('s1/MAT','SystemSampleTime'));
Here s1 is the Simulink model name, MAT is the name of the MATLAB function block.

Products

Release

R2021b

Asked:

on 23 Dec 2021

Answered:

on 31 Dec 2021

Community Treasure Hunt

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

Start Hunting!