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

6 visualizaciones (últimos 30 días)
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!

Respuestas (1)

Bob Blaine
Bob Blaine el 21 de Jul. de 2017
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 comentarios
Bob Blaine
Bob Blaine el 4 de Ag. de 2017
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 el 6 de Ag. de 2017
Editada: tahec el 7 de Ag. de 2017
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".

Iniciar sesión para comentar.

Categorías

Más información sobre Maintain or Transition figure-Based Apps en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by