How can I get the figure handle of a Scope window programmatically, given only the Scope block's full path name?

I've got the full path name of a Scope block as it is returned by find_system(...), e.g. 'myModel/Scope1'.
In order to manipulate the corresponding Scope window using set(...), I need the handle of that window. Is there some way to call get_param('myModel/Scope1', unknownparameternamehere) to get that window handle?
What I already tried (but do not consider a solution):
(1) A workaround I'm currently using is to call set_param('myModel/Scope1','Open','On'), which sometimes makes that window the current figure, so I can use "gcf" afterwards to get the handle. But that doesn't work safely and also has unwanted side-effects.
(2) Using findobj(0,'type','figure','tag','SIMULINK_SIMSCOPE_FIGURE') to get an array of all Scope window handles doesn't help because afterwards I can't identify the Scope window in that array by its 'Name' field only: There might be 'myModel/Scope1' and 'myModel/sub/Scope1' or 'anotherModel/Scope1', so 'Name' is not unique.

3 commentaires

Hi creepydog,
Do I understand correctly that you're trying to get the Matlab figure handle of a scope window so that you can then control its figure properties in the "usual" way?
In R2022a with a single scope window open, this command returned empty
findobj(0,'type','figure','tag','SIMULINK_SIMSCOPE_FIGURE')
and this command
findobj
only returned the Graphics root.
Maybe there's been change in behavior so that Scope windows are no longer handle graphics objects? Or is there some block property that I need to apply at the Simulink block level to make that findobj command work?
Hi Paul,
my question is more than 6 years old, so it took me some effort to find the script where I tried those things in 2017. I found that this script starts with this command:
set(0,'ShowHiddenHandles','On');
I can't remember what that does. A quick test using R2022b shows: Without that I get the same result you describe [=empty GraphicsPlaceholder array], but after setting this to 'On', findobj() returns the figure handle of my Scope window. This allows me to set(..., 'position', ...) for example.
The background of my question in 2017 was: I had many scope windows and I wanted to have them arranged (=positioned) on my (second) screen (semi-)automatically and nicely without overlap. Example: By defining this variable, the script was to arrange the scope windows in three columns using the specified percentages of the column height for each window.
scopes = {...
{'foo scope', 2, 30} % 'foo scope' should use 30% of column 2
{'bar scope', 3, 60}
{'speed scope', 1, 50}
{'force scope', 1, 50}
{'temperature scope', 2, 70}
{'whatever scope', 3, 40}
};
That worked quite well in the end. The solution I used is not nice, though. It renames the scope blocks temporarily to make them unique and then finds their window handles using findobj(), then renames them back.
Yes, ShowHiddenHandles did the trick.
I'm kind of (maybe not really) surprised there isn't a way to correlate figure handles with scope handles. I guess they really want users to only use the ScopeConfiguration object. If that doesn't offer desired functionality, I guess one can always submit an enhancement request.

Connectez-vous pour commenter.

 Réponse acceptée

There does not seem to be a documented way of getting handle to the scope window from the full-name of the scoped block.
However, as I understand, you wish to manipulate he display properties of the scope window using the handle. This can be done by obtaining 'ScopeConfiguration' object of the scope block. You can refer the following documentation pages which explain this workflow.

5 commentaires

Then perhaps someone should document it ...
Now I tried the ScopeConfiguration object (which I didn't know before). It allows me to get/set the "Position" of the Scope window, but not its "OuterPosition", which is possible using set(handle,...).
Still this helped me a lot, thanks!
I found a roundabout way of getting the handle to the scope window.
Assuming that the scope block is named 'Scope 1' in the model named 'testModel', the following sequence of commands should work.
>> open_system('testModel/Scope 1'); % opens the scope window so that it can be grabbed
>> scopeWindowH=findall(0,'Type','figure','Name','Scope 1'); % fetch the handle
See my original question, part "what I already tried", #2: 'Scope 1' might not be unique. So findall/findobj returns more than one handle and I can't find out which one's the right one.
As of MATLAB R2025a, the underlying graphics of the Simulink Scope block have changed. You cannot access or modify the Simulink Scope block through the MATLAB Graphics API.
To display the scope data in a MATLAB Graphics window, use the Print to Figure functionality in the File tab of the Scope block toolstrip.
Please see the release notes for more information: Scope - Display signals generated during simulation - Simulink.

Connectez-vous pour commenter.

Plus de réponses (1)

%In this code, I get the names and paths to all scopes in a model, then open them one by one
mdl='..................'; %your simulink model name without extension
xxx=find_system(mdl,'LookUnderMasks','on','IncludeCommented','on',...
'AllBlocks','on','BlockType','Scope','DefaultConfigurationName',...
%In this code, I get the names and paths to all scopes in a model, open them one by one
mdl='..................'; %your simulink model name without extension
xxx=find_system(mdl,'LookUnderMasks','on','IncludeCommented','on',...
'AllBlocks','on','BlockType','Scope','DefaultConfigurationName',...
'Simulink.scopes.TimeScopeBlockCfg')
yyy=size(xxx);
loop_end=yyy(1);
for iii=1:loop_end
path_to_scope=xxx{iii};
aaa=findstr(xxx{iii},'/'); %finding the locations of the '/' separatore
mmm=max(aaa); % finding the location of the last '/'
scope_name=xxx{iii}(mmm+1:end); %capturing the name only of the scope
%open this scoppe
open_system(path_to_scope)
hs = findall(0,'Name',scope_name);
end
%This code closes all open scopes
shh = get(0,'ShowHiddenHandles');
set(0,'ShowHiddenHandles','On');
hscope = findobj(0,'Type','Figure','Tag','SIMULINK_SIMSCOPE_FIGURE');
close(hscope);
set(0,'ShowHiddenHandles',shh);

2 commentaires

If I'm not mistaken, your code identifies the window handles based on the block names which are not necessarily unique. See me question, "what I already tried", part 2.
Yes, it is true...may be I have put the wrong answer to your question while I was trying to answer another question asking for how to get the scope name from the path to the scope! Thanks for the notification

Connectez-vous pour commenter.

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by