Finding the sample time of a Simulink signal/line programmatically with Simulink.B​lock.getSa​mpleTimes

I am trying to find the actual sample time of a signal/line in a Simulink model, programmatically, using Matlab commands.
So far I can't do it, although the information must be there, because Simulink can create the "Sample Time Color" overlay.
A simple demo model is attached, "FindSignalSampleTime.slx".
Clearly Simulink knows that the signal/line "Signal_1ms" has a discrete sample rate of 1e-3 s (1ms).
Now the challenge is to determine the sample time of that signal/line, called "Signal_1ms" programmatically. In this example I KNOW that the answer is 1e-3 s. But, so far, I cannot find a way to find it using code. How can it be done?
The handle of the signal can be found by:
loggedSignalName = 'Signal_1ms';
loggedSignalWire.Handle = find_system(modelName, ...
'FollowLinks','on', ...
'FindAll', 'on', ...
'LookUnderMasks','all',...
'type', 'line', ...
'Name', loggedSignalName);
fprintf('loggedSignalWire.Handle = %g\n', loggedSignalWire.Handle);
which produces
loggedSignalWire.Handle = 11.0004
I would like to now do:
ts = Simulink.Block.getSampleTimes(loggedSignalWire.Handle);
but that throws an error "The first argument to Simulink.Block.getSampleTimes must be a block".
Next I can find the "Source Port" that the line/signal is created by:
loggedSignalWireSrcPort.Handle = get_param(loggedSignalWire.Handle,'SrcPortHandle');
fprintf('loggedSignalWireSrcPortHandle = %g\n', loggedSignalWireSrcPort.Handle);
which produces
loggedSignalWireSrcPortHandle = 14.0004
Again, I would like to try:
ts = Simulink.Block.getSampleTimes(loggedSignalWireSrcPortHandle);
but again that throws an error "The first argument to Simulink.Block.getSampleTimes must be a block".
So finally I can find the block that contains that source port, and feeds the signal/line:
loggedSignalWireSrcPort.DataLoggingSampleTime = get_param(loggedSignalWireSrcPort.Handle, 'DataLoggingSampleTime');
loggedSignalWireSrcPort.ParentBlockName = get_param(loggedSignalWireSrcPort.Handle, 'Parent');
loggedSignalWireSrcPort.ParentBlockHandle = getSimulinkBlockHandle(loggedSignalWireSrcPort.ParentBlockName);
loggedSignalWireSrcPort.PortNumber = get_param(loggedSignalWireSrcPort.Handle, 'PortNumber');
fprintf('loggedSignalWireSrcPort.DataLoggingSampleTime = %s\n', loggedSignalWireSrcPort.DataLoggingSampleTime); % -1. Not much help.
fprintf('loggedSignalWireSrcPort.ParentBlockName = %s\n', loggedSignalWireSrcPort.ParentBlockName); % 'FindSignalSampleTime/SignalGeneration'
fprintf('loggedSignalWireSrcPort.ParentBlockHandle = %g\n', loggedSignalWireSrcPort.ParentBlockHandle); % 'slexVariableTransportDelayR2021bWithSimPowerSystems/vGain Library Variant Subsystem'
fprintf('loggedSignalWireSrcPort.PortNumber = %d\n', loggedSignalWireSrcPort.PortNumber); % 2
fprintf('Signal "%s" comes from output port %d of block handle %g with name "%s".\n', loggedSignalName, loggedSignalWireSrcPort.PortNumber, loggedSignalWireSrcPort.ParentBlockHandle, loggedSignalWireSrcPort.ParentBlockName);
which produces:
Signal "Signal_1ms" comes from output port 1 of block handle 4.00037 with name "FindSignalSampleTime/SignalGeneration".
Now finally a call to Simulink.Block.getSampleTimes() can return without error, using the handle for that whole block:
ts = Simulink.Block.getSampleTimes(loggedSignalWireSrcPort.ParentBlockHandle);
but the information I need is not there. The information only contains:
ts.Description = "Multirate"
ts.Value = ""
I would like the information in ts to tell me the sample rates for each output port, but it doesn't, it just says that the block is "Multirate", and that's not much help in this instance.
So I am still stuck with my problem. How can I determine the sample time of the signal/line? Simulink must know, but I can't find any valid commands/syntax that will tell me.

Answers (2)

Hi Andrew,
The sample time is defined for a block, not a signal line. The sample time of the data on a signal line depends on the block it is connected to. The sample time of a particular block in a Simulink model can be obtained using the following command:
ts = Simulink.Block.getSampleTimes(block)
The “block” input argument must be a block handle and not a port handle. The output "ts" of this command is a “Simulink.SampleTime” object. You can refer to the following documentation to understand more about the command:
You can refer to the following documentation to understand more about the “Simulink.SampleTime” object returned as an output:
I hope this answers your query.

1 Comment

In the original question I describe exactly that, and how it does not produce the required information. The block reports only that it is "Multirate".
Doing:
ts = Simulink.Block.getSampleTimes(block);
with block set to 'FindSignalSampleTime/SignalGeneration' in the example produces:
and the information required is not there.
What I need to find out is the actual sample rates applied to each of the block output ports, but this does not seem to be possible as I cannot call Simulink.Block.getSampleTimes() on the source block output ports themselves.
But I am hoping that there is a way to extract the information, hence this question

Sign in to comment.

Hi Andrew,
If the block has multiple outputs, like in your case, check out
ts.ComponentSampleTimes
which should be an array of SampleTime obejcts, one for each output of the block.
Also, check out CompiledSampleTime at this doc page. Not sure if/how it differs from Simulink.Block.getSampleTimes.

8 Comments

Thanks Paul, this is helping but I am not there yet.
The first part of your answer doesn't help:
ts.ComponentSampleTimes
is an emply (0x0) SampleTime array, it doesn't have two SampleTime objects, one for each output of the block! If it did have those two objects, that would be exactly what I want, but it doesn't!
ts =
SampleTime with properties:
Value: []
Description: 'Multirate'
ColorRGBValue: [0.9100 0.8200 0.3200]
Annotation: 'M'
OwnerBlock: []
ComponentSampleTimes: [0×0 Simulink.SampleTime]
The second part of your answer gets closer.
The doc page
has an entry which describes, for example,
ts = get_param(block,"CompiledSampleTime")
If I do that on my multirate block in the original example it returned:
ts = get_param('FindSignalSampleTime/SignalGeneration',"CompiledSampleTime")
ts =
2×1 cell array
{[1.0000e-03 0]}
{[ 0.0100 0]}
which (I assumed) included the [sample time, offset time], I assume of port 1 and 2 in order.
So now I make the model a bit more complex, just to check if
ts = get_param(block,"CompiledSampleTime")
returns an array that matches output ports.
Unfortunately it doesn't. It returns an array that matches "components" of the block, and these don't have a 1:1 correspondence with output port numbers.
Attached to this post are an updated slx model and Matlab script.
In this example the desired signal to find the properties of is again "Signal_1ms", which is output port 3 of the block.
If I do:
blockCompiledSampleTimes = get_param('FindSignalSampleTime/SignalGeneration',"CompiledSampleTime");
fprintf('blockCompiledSampleTimes :\n');
disp(blockCompiledSampleTimes)
I get
blockCompiledSampleTimes :
{[1.0000e-05 0]}
{[1.0000e-03 0]}
{[ 0.0100 0]}
{[ 0.1000 0]}
This has 4 elements, which is not the same as the number of output ports. It shows me that I don't yet understand the relationship between the elements in blockCompiledSampleTimes and the output port numbers. The element of blockCompiledSampleTimes I am looking for, in this example, is blockCompiledSampleTimes{2}, but I need to know how port 3 maps to element 2 of blockCompiledSampleTimes.
Not sure why things aren't working with your model. Works here with a simple model.
% Build a simple model, with two Sine Wave blocks passing through a
% subsystem and feeding two scopes. Use differnt sample times for the Sine
% Wave blocks
bdclose('all');
hsys = new_system('andrew');
% uncomment this line to open the model in a Simulink canvas
%open_system(hsys);
hsin1 = add_block('simulink/Sources/Sine Wave','andrew/Sine1','SampleTime','0.1');
hsin2 = add_block('simulink/Sources/Sine Wave','andrew/Sine2','SampleTime','0.5');
hsub = add_block('simulink/Ports & Subsystems/Subsystem','andrew/subsys');
hin2 = add_block('andrew/subsys/In1','andrew/subsys/In2');
hout2 = add_block('andrew/subsys/Out1','andrew/subsys/Out2');
hscope1 = add_block('simulink/Sinks/Scope','andrew/scope1');
hscope2 = add_block('simulink/Sinks/Scope','andrew/scope2');
PH = 'PortHandles';
add_line(hsys, ...
get_param(hsin1,PH).Outport,...
get_param(hsub,PH).Inport(1));
add_line(hsys, ...
get_param(hsin2,PH).Outport,...
get_param(hsub,PH).Inport(2));
add_line(hsys, ...
get_param(hsub,PH).Outport(1),...
get_param(hscope1,PH).Inport);
add_line(hsys, ...
get_param(hsub,PH).Outport(2),...
get_param(hscope2,PH).Inport);
add_line(hsub, ...
get_param(hin2,PH).Outport,...
get_param(hout2,PH).Inport);
% get the sample times of the outputs of the subsystem block using
% getSampleTimes
ts = Simulink.Block.getSampleTimes('andrew/subsys')
ts =
SampleTime with properties: Value: [] Description: 'Multirate' ColorRGBValue: [0.5451 0.4000 0.0235] Annotation: 'M' OwnerBlock: [] ComponentSampleTimes: [1x2 Simulink.SampleTime]
ts.ComponentSampleTimes
ans = 1x2 SampleTime array with properties:
Value Description ColorRGBValue Annotation OwnerBlock ComponentSampleTimes
ts.ComponentSampleTimes(1)
ans =
SampleTime with properties: Value: [0.1000 0] Description: 'Discrete 1' ColorRGBValue: [1 0.2706 0.2275] Annotation: 'D1' OwnerBlock: [] ComponentSampleTimes: [0x0 Simulink.SampleTime]
ts.ComponentSampleTimes(2)
ans =
SampleTime with properties: Value: [0.5000 0] Description: 'Discrete 2' ColorRGBValue: [0.2275 0.7843 0.1922] Annotation: 'D2' OwnerBlock: [] ComponentSampleTimes: [0x0 Simulink.SampleTime]
% get the sample times of the output of the subsystem block using
% get_param. I think, but am not positive, that the feval calls are
% required.
feval('andrew',[],[],[],"compile");
dt = get_param(hsub,"CompiledSampleTime");
feval('andrew',[],[],[],"term");
dt
dt = 2x1 cell array
{[0.1000 0]} {[0.5000 0]}
Not sure why neither of these approaches work with your model.
Are all three outputs of the SignalGeneration block scalar signals?
Checking the model posted in this comment there certainly seems to be something strange. Is there a chance that the screen cap in that comment is not from the model you attached? But even if that's the case, there is still strange behavior.
load_system('FindSignalSampleTime');
The SignalGeneration block has three output signals
ph = get_param('FindSignalSampleTime/SignalGeneration','PortHandles').Outport
ph = 1×3
286.0002 287.0002 288.0002
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Each output is a scalar signal
feval('FindSignalSampleTime',[],[],[],"compile");
Warning: 'Input Port 2' of 'FindSignalSampleTime/SignalGeneration' is not connected.
Warning: 'Output Port 1' of 'FindSignalSampleTime/SignalGeneration' is not connected.
portdim = get_param(ph,"CompiledPortDimensions");
feval('FindSignalSampleTime',[],[],[],"term");
portdim
portdim = 3x1 cell array
{[1 1]} {[1 1]} {[1 1]}
But the names aren't what's shown in the screen cap, checked two different ways
get_param(ph,'Name')
ans = 3x1 cell array
{0x0 char } {'Signal_1ms' } {'Signal_10ms'}
get_param('FindSignalSampleTime/SignalGeneration','OutputSignalNames')
ans = 1x3 cell array
{0x0 char} {'Signal_1ms'} {'Signal_10ms'}
I really don't understand why ts has four elements
ts = Simulink.Block.getSampleTimes('FindSignalSampleTime/SignalGeneration')
Warning: 'Input Port 2' of 'FindSignalSampleTime/SignalGeneration' is not connected.
Warning: 'Output Port 1' of 'FindSignalSampleTime/SignalGeneration' is not connected.
ts =
SampleTime with properties: Value: [] Description: 'Multirate' ColorRGBValue: [0.5451 0.4000 0.0235] Annotation: 'M' OwnerBlock: [] ComponentSampleTimes: [1x4 Simulink.SampleTime]
ts.ComponentSampleTimes(1)
ans =
SampleTime with properties: Value: [1.0000e-05 0] Description: 'Discrete 1' ColorRGBValue: [1 0.2706 0.2275] Annotation: 'D1' OwnerBlock: [] ComponentSampleTimes: [0x0 Simulink.SampleTime]
ts.ComponentSampleTimes(2)
ans =
SampleTime with properties: Value: [1.0000e-03 0] Description: 'Discrete 2' ColorRGBValue: [0.2275 0.7843 0.1922] Annotation: 'D2' OwnerBlock: [] ComponentSampleTimes: [0x0 Simulink.SampleTime]
ts.ComponentSampleTimes(3)
ans =
SampleTime with properties: Value: [0.0100 0] Description: 'Discrete 3' ColorRGBValue: [0.0549 0 1] Annotation: 'D3' OwnerBlock: [] ComponentSampleTimes: [0x0 Simulink.SampleTime]
ts.ComponentSampleTimes(4)
ans =
SampleTime with properties: Value: [0.1000 0] Description: 'Discrete 4' ColorRGBValue: [0.0549 0 1] Annotation: 'D4' OwnerBlock: [] ComponentSampleTimes: [0x0 Simulink.SampleTime]
And I don't understand why the CompiledSampleTime doesn't match the getSampleTimes.
EDIT (4 Nov 2024): This block of code calls get_param on the porthandles to get the CompiledSampleTime, which AFAICT is not documented behavior, even though it returns a result, though I'm not sure the result is correct.
feval('FindSignalSampleTime',[],[],[],"compile");
Warning: 'Input Port 2' of 'FindSignalSampleTime/SignalGeneration' is not connected.
Warning: 'Output Port 1' of 'FindSignalSampleTime/SignalGeneration' is not connected.
dt = get_param(ph,"CompiledSampleTime");
feval('FindSignalSampleTime',[],[],[],"term");
dt
dt = 3x1 cell array
{[ 0.1000 0]} {[1.0000e-03 0]} {[ 0.0100 0]}
The documented behavior is to get the CompliledSampleTime using the block as the input to get_param.
feval('FindSignalSampleTime',[],[],[],"compile");
Warning: 'Input Port 2' of 'FindSignalSampleTime/SignalGeneration' is not connected.
Warning: 'Output Port 1' of 'FindSignalSampleTime/SignalGeneration' is not connected.
dt = get_param('FindSignalSampleTime/SignalGeneration',"CompiledSampleTime");
feval('FindSignalSampleTime',[],[],[],"term");
dt
dt = 4x1 cell array
{[1.0000e-05 0]} {[1.0000e-03 0]} {[ 0.0100 0]} {[ 0.1000 0]}
And now we are back to four outputs, which I think match the output of getSampleTimes.
Nothing makes senses.
I was working in R2021b.
I attach it here (the same model) for clarification as "FindSignalSampleTime_R2021b.slx"
and also the screenshot FindSignalSampleTime_R2021b.PNG.
Working in R2021b and following your commands above, I get different answers to you. Ultimately the "dt" variable actually seems to have the information I need! :)
clear
close all
bdclose all
open_system('FindSignalSampleTime_R2021b');
ph = get_param('FindSignalSampleTime_R2021b/SignalGeneration','PortHandles').Outport
ph = 36.0002 29.0002 31.0002
feval('FindSignalSampleTime_R2021b',[],[],[],"compile");
portdim = get_param(ph,"CompiledPortDimensions");
feval('FindSignalSampleTime_R2021b',[],[],[],"term");
portdim
portdim =
3×1 cell array
{[1 1]}
{[1 1]}
{[1 1]}
get_param(ph,'Name')
ans =
3×1 cell array
{'Signal_100ms'}
{'Signal_10ms' }
{'Signal_1ms' }
get_param('FindSignalSampleTime_R2021b/SignalGeneration','OutputSignalNames')
ans =
1×3 cell array
{'Signal_100ms'} {'Signal_10ms'} {'Signal_1ms'}
These look correct to me.
ts = Simulink.Block.getSampleTimes('FindSignalSampleTime_R2021b/SignalGeneration')
ts =
SampleTime with properties:
Value: []
Description: 'Multirate'
ColorRGBValue: [0.9100 0.8200 0.3200]
Annotation: 'M'
OwnerBlock: []
ComponentSampleTimes: [0×0 Simulink.SampleTime]
I still have ComponentSampleTimes as an empty structure :(
dt = get_param(ph,"CompiledSampleTime");
feval('FindSignalSampleTime_R2021b',[],[],[],"term");
dt
dt =
3×1 cell array
{[ 0.1000 0]}
{[ 0.0100 0]}
{[1.0000e-03 0]}
but HERE, dt{1,0}, dt{2,0} and dt{3,0} appear to be exactly the things I need, being the sample times of output ports 1, 2 and 3.
This LOOKS like it provies a way through, to answer my original question.
What is curious to me now is that I searched very hard to find this information, and couldn't. Now I see why, and the reason I couldn't find a solution is that
get_param(ph,"CompiledSampleTime");
is a "hidden" and undocumented query!
In my case, and original question, I was looking for the sample time of the 'Signal_1ms' line/signal, which I can discover comes from output port 3 of the block, with "source port" (output port) handle
ph(3) = 31.0002
To try and fine out what properties are available for the output port of interest, I used the commands as below:
loggedSignalWireSrcPort.Handle = ph(3);
loggedSignalWireSrcPort.ListOfPropertiesAsStruct = get_param(loggedSignalWireSrcPort.Handle, 'ObjectParameters');
loggedSignalWireSrcPort.ListOfProperties = fieldnames(loggedSignalWireSrcPort.ListOfPropertiesAsStruct);
for k = 1:length(loggedSignalWireSrcPort.ListOfProperties)
loggedSignalWireSrcPort.AllProperties.(loggedSignalWireSrcPort.ListOfProperties{k}) = get_param(loggedSignalWireSrcPort.Handle, loggedSignalWireSrcPort.ListOfProperties{k});
end
loggedSignalWireSrcPort.ListOfPropertiesAsStruct.CompiledSampleTime
loggedSignalWireSrcPort.AllProperties.CompiledSampleTime
This produces a whole swathe of available properties that can be queried and set for the output port. It also populates the structure loggedSignalWireSrcPort.AllProperties with a handily viewable set of properties of (apparently) all the properties of that output port. BUT, "CompiledSampleTime" is not on the list! It appears to be a secret / easter-egg / undocumented feature !
Now, only because Paul has shown it being used, I think I can construct code to do what I need. But to do so, I have to use information that is apparently undocumented and "secret". Only a Matlab insider could have known that "CompiledSampleTime" is an available property for an output port handle?
Now interesting moving up from Matlab R2021b to R2024b ... If I reload the same model (renamed to FindSignalSampleTime_R2024b.slx)
Paul's example commands I run again.
1) The handle numbers are different, but that doesn't really matter as long as the methods carry through.
2) Interestingly, the ts.ComponentSampleTimes field is now populated with 4 SampleTimes elements. However, because there are 4 of them, and because the information in them is vague, I wouldn't know which of the four to use.
3) The (?secret?) command "dt = get_param(ph,"CompiledSampleTime");" seems to be the key operation that gives me the information I need, if I use the correct ph() index/value that is the output port handle that produces the signal/line.
On this basis, for both R2021b and R2024b, I think I can construct a sequence of commands to do this now. I will try in the next comment.
clear
close all
bdclose all
open_system('FindSignalSampleTime_R2024b');
ph = get_param('FindSignalSampleTime_R2024b/SignalGeneration','PortHandles').Outport
feval('FindSignalSampleTime_R2024b',[],[],[],"compile");
portdim = get_param(ph,"CompiledPortDimensions");
feval('FindSignalSampleTime_R2024b',[],[],[],"term");
portdim
get_param(ph,'Name')
get_param('FindSignalSampleTime_R2024b/SignalGeneration','OutputSignalNames')
ts = Simulink.Block.getSampleTimes('FindSignalSampleTime_R2024b/SignalGeneration')
ts =
SampleTime with properties:
Value: []
Description: 'Multirate'
ColorRGBValue: [0.5451 0.4000 0.0235]
Annotation: 'M'
OwnerBlock: []
ComponentSampleTimes: [1×4 Simulink.SampleTime]
for k=1:length(ts.ComponentSampleTimes)
fprintf('Component sample time %d : [%g %g]\n', k, t.ComponentSampleTimes(k).Value);
end
Component sample time 1 : [1e-05 0]
Component sample time 2 : [0.001 0]
Component sample time 3 : [0.01 0]
Component sample time 4 : [0.1 0]
So Component sample time 2 is the one I want, but how can I know that that's from port 3 when it's in index 2, and there are 4 entries?
There's no clue inside ts.ComponentSampleTimes(2) that it maps to port 3.
ts.ComponentSampleTimes(2)
ans =
SampleTime with properties:
Value: [1.0000e-03 0]
Description: 'Discrete 2'
ColorRGBValue: [0.2275 0.7843 0.1922]
Annotation: 'D2'
OwnerBlock: []
ComponentSampleTimes: [0×0 Simulink.SampleTime]
feval('FindSignalSampleTime_R2024b',[],[],[],"compile");
dt = get_param(ph,"CompiledSampleTime");
feval('FindSignalSampleTime_R2024b',[],[],[],"term");
dt
dt =
3×1 cell array
{[ 0.1000 0]}
{[ 0.0100 0]}
{[1.0000e-03 0]}
But it still looks fairly convincing that I can assume that dt(3,1) is the value relating to output port 3, and I know from other commands that port 3 feed signal 'Signal_1ms'
I don't understand why your 2024b results are different than your 2021b results. I can only run 2024b here on Answers, and now I gather I was running it on a model built in 2021b (not that that should make a difference, but still). Maybe someone else will see this thread and try to work with an ealier version. I also don't understand why when I ran with with 2024b here on Answers (with th 2021b model) I got two warnings about unconnected ports, but you didn't when you ran it on your local installation. That is very odd if we were both working with the same model.
CompiledSampleTimes is not undocumented. It is shown on at Programmatically Specify Block Parameters and Properties in the Common Block Properties section, and according to that doc page requires the feval statements before and after the call to get_param to access that property. Now I realize that in working with your model above (but not with my toy model called 'andrew') I was calling get_param(ph,"CompiledSampleTime"). That is incorrect (even if it worked); the first argument is supposed to be a block, not a vector of port handles (at least as far as I can tell). I edited that comment to call get_param the correct way and got strange results.
There certainly seem to be lots of odd happenings in this thread, particularly, but not only, with getSampleTimes. Maybe open a case with TechSupport?
I think, as in I'm pretty sure, that I misunderstood the intended functionality of Simulink.Block.getSampleTimes. I'm now pretty sure that's not the function to use.
I also agree with you that CompiledSampleTime is not a shown property of a port handle (I can't find any documenation on port handle properties at all).
There has to be a way to do what you want w/o resorting to using undocumented features and unshown object parameters ....
Hi Paul,
Thanks for the updates. I'm glad that my original question wasn't stupid. It seems to have raised a number of questions.
In summary, right now, basically there is an answer, but it seems to rely on an undocumented parameter of a source port handle. The procedure which seems to work is:
1) Execute the "compilation" phase of a simulation. This will leave the model in a "Compiled" and part-simulated state. After extracting all the sample time information, do a clean termination using the "term" command, to put the model back into a "Ready" state.
feval(modelName, [], [], [], "compile");
The model should now say "Compiled".
2) Find the wire handle for the wire/signal name (loggedSignalName) that you wish to find the sample time of, in model modelName.
signalWireHandle = find_system(modelName, ...
'FollowLinks','on', ...
'FindAll', 'on', ...
'LookUnderMasks','all',...
'type', 'line', ...
'Name', loggedSignalName);
3) Find the relevant "SrcPortHandle" for the wire/signal (signalWireHandle) that you wish to find the sample time of. This is the output port handle, related to the block that produces the line/signal of interest.
sourcePortHandle = get_param(signalWireHandle,"SrcPortHandle");
4) Take the handle of that output port and execute the following statement:
sampleTime = get_param(sourcePortHandle, "CompiledSampleTime");
Repeat 2-4 for all the signals of interest.
5) Clean termination of the simulation that was "started" with the compilation phase. If you don't do this it leaves the model in a "compiled" and part-simulated state.
feval(modelName, [], [], [], "term");
Notes:
1) This procedure uses an essentially undocumented parameter "CompiledSampleTime" of a port handle. You don't find it on the list of available parameters if you do
listOfPropertiesAsStruct = get_param(sourcePortHandle, "ObjectParameters");
However the parameter "CompiledSampleTime" is a documented property of a block.
2) You must put the model into a compiled state. If you just do a normal model update command:
set_param(modelName,'SimulationCommand','update');
it is not enough, and you will get "NaN" results.
3) If you want, you can add dialogue about where the signal comes from, which is nice to see:
parentBlockName = get_param(sourcePortHandle, 'Parent');
portNumber = get_param(sourcePortHandle, 'PortNumber');
fprintf('Signal "%s" comes from output port %d of block "%s" and has sample time %g s with offset %g s.\n', ...
loggedSignalName, portNumber, parentBlockName, sampleTime(1), sampleTime(2))
4) You can attempt to find the information and sample times for a block handle using commands like:
parentBlockHandle = getSimulinkBlockHandle(parentBlockName);
ts = Simulink.Block.getSampleTimes(parentBlockHandle)
for k=1:length(ts.ComponentSampleTimes)
fprintf('Parent block "%s" : Component sample time %d : [%g %g]\n', parentBlockName, k, ts.ComponentSampleTimes(k).Value);
end
This is the route that documentation may lead you.
However the information in ts.ComponentSampleTimes doesn't appear to be helpful.
Some blocks like signal sources return 0x0 SampleTime values in ts.ComponentSampleTimes.
Other blocks return 1xN SampleTime values in ts.ComponentSampleTimes, where N is not equal to the number of output ports, input ports, or input+output ports, but contains a list of sample times that are used anywhere within the block. The order of the SampleTime values is not obvious and certainly doesn't match the order of the output ports. Therefore you can't tell which of the ts.ComponentSampleTimes entries is connected to which output or signal from the block.
Tests I did in Matlab 2021b and 2024b suggests that 2021b can return 0x0 SampleTime values but 2024b can return 1xN SampleTime values, for, apparently, the same models, suggesting that the procedure is not fully stable.

Sign in to comment.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 29 Oct 2024

Commented:

on 5 Nov 2024

Community Treasure Hunt

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

Start Hunting!