Finding the sample time of a Simulink signal/line programmatically with Simulink.Block.getSampleTimes
69 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponses (2)
Sumukh
le 30 Oct 2024 à 7:32
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 commentaire
Paul
le 30 Oct 2024 à 16:57
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 commentaires
Paul
le 5 Nov 2024 à 4:40
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 ....
Voir également
Catégories
En savoir plus sur Model Verification dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!