Function 'subsindex' is not defined for values of class 'matlab.gr​aphics.Gra​phicsPlace​holder'

6 次查看(过去 30 天)
I have the following Figure
classdef gui < matlab.apps.AppBase
...
function app = gui
% Construct app
end
...
properties (Access = public)
myFuncRef = @myFun
end
...
function myFun(app)
% do something
end
...
end
in which I have defined the method
myFun
If the Figure is running, that is it's showing a window, how can I invoke the method "MyFun" from the Command Window of MATLAB or MATLAB Fcn Block in Simulink ? In the simulink I tried with
h = findobj(0, 'type', 'figure');
funcRef = get(h, 'myFuncRef');
funcRef(h);
but I get the error
An error occurred while running the simulation and the simulation was terminated Caused by: Function 'subsindex' is not defined for values of class 'matlab.graphics.GraphicsPlaceholder'.
Thanks in advance!

回答(1 个)

Bob Blaine
Bob Blaine 2017-7-21
Hi tahec,
The error message is telling you that findobj didn't find any figures on the graphics root, and couldn't resolve the function call. If you open MATLAB with no graphics and just execute your "h = ..." block of code you will get that error.
AppBase is the base class for an App Designer application and usually contains a UIFigure, which is what shows up on the screen. Even if you are creating a UIFigure in the constructor, it won't show up in a findobj, but it will show up in findall:
h = findall(0,'type', 'figure')
That will get you halfway there. One other thing you will have to do is put a handle to the gui object in the UIFigure's UserData property. You can then call its function:
if ~isempty(h) then
h.UserData.myFun()
end
Having said all of this, I'm hoping that this is for debugging, or fiddling with a parameter. You'd probably want to code something into the graphics GUI as a permanent functionality.
  3 个评论
Bob Blaine
Bob Blaine 2017-8-4
Hi tahec,
Sorry about being a little vague, but based on the code you posted, I was assuming that you were constructing the UIFigure in the constructor of your gui object the way App Designer does:
classdef MyObject < matlab.apps.AppBase
properties
UIFigure matlab.ui.Figure
end
methods
function gui = MyObject
gui.UIFigure = matlab.ui.Figure;
% Set the Userdata on the UIFigure to gui
gui.UIFigure.UserData = gui;
end
end
end
If you aren't creating a UIFigure, then I'm not sure where you can get a handle to your gui object. That may require someone more Simulink savvy to help you out.
tahec
tahec 2017-8-6
编辑:tahec 2017-8-7
Hi Bob,
I show you what I did.
This is the class
classdef gui < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
AlternatingControllersUIFigure matlab.ui.Figure
f_ref = @f;
...
end
methods (Access = private)
function f(app)
...
end
% Code that executes after component creation
function startupFcn(app)
app.AlternatingControllersUIFigure.UserData = app;
end
% Create UIFigure and components
function createComponents(app)
app.AlternatingControllersUIFigure = uifigure;
...
end
end
methods (Access = public)
% Construct app
function app = gui
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.AlternatingControllersUIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
end
end
In the block diagram, I have the following block
in which I wrote the following code
When I run the overall app (gui + block diagram), simulink tells me
What is the matter? Note that I am not obligated to use the block "MATLAB Function".

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Maintain or Transition figure-Based Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by