1
votes

I've been working on a GUI for a computing program in Matlab, that program is implemented in a Object-oriented manner. So I'm thinking how to integrate the GUI code created by GUIDE to the class to keep the conformity.

First I searched the Stackoverflow to find out the similar question, and then I got one Click.

I tried the method mentioned in that answer, and failed. So anyone could give me some more details and advise would be really helpful!

My code is orgnized like that:

classdef A < handle

methods

  function obj = A(varargin)

    %some code
  end

  function Aplot(obj,varargin)
    %some code
    obj.fh = Aview(obj)
  end

  function varargout = Aview(varargin) 
     % this is the code generated by GUIDE in a seperate file

     % associated with AView_OpeningFcn(hObject, eventdata, handles, varargin)

     % and component(edit, popupmenu,etc) callback functions

  end

Then I got lots of error for every component(buttion,popupmenu) in windows, typically like this:

Undefined function 'AView' for input arguments of type 'char'.

Error in @(hObject,eventdata)AView('popupmenu1_CreateFcn',hObject,eventdata,guidata(hObject)

Error using struct2handle

Error while evaluating uicontrol CreateFcn

How can I successfully pass the object of class A to the GUI function AView window, in case I need some data structure inside object A?

Thanks in advance:)


More comments: I didn't copy the file generated by GUIDE into the Class A. But just put the GUIDE-generated in a folder which name is @A, as same as A.m. In this way, the function generated by GUIDE will be regard as the method of class A

Reference: search "Methods In Separate Files" in http://www.mathworks.com/help/matlab/matlab_oop/specifying-methods-and-functions.html#brqy3km-15

2
I'm not sure I understand, did you copy code from the guide function into your class?Trogdor
No, I created a folder which name is @A, and put the guide generated file in this folder, in this way, the functions created by GUIDE will be regarded as the method in class A. reference: search "Methods In Separate Files" in mathwork websiteRisheng

2 Answers

1
votes

It's very possible to create a MATLAB GUI in an OO style. From the naming of your methods (e.g. View), it looks like you're attempting an MVC (model-view-controller) pattern, which is also very possible.

But if you're doing that, then it's really time to drop GUIDE.

GUIDE-generated code is fine for very quick-and-simple GUIs, but it's very old-fashioned in its structure, and goes against many principles of an OO design (e.g. all the handles to the GUI components are stored in what is effectively a big old global variable, which needs to get passed around and constantly updated). It would only be possible to integrate GUIDE-generated code into an OO application at great cost to its structure and design.

Instead, take a look at GUI Layout Toolbox. This a free toolbox, authored by MathWorks consultants (although not supported as an official MathWorks product). It includes functionality for additional GUI techniques such as layouts and resizing, and the (very thorough) documentation includes some excellent examples of how to implement MATLAB GUIs in an OO (MVC) pattern.

0
votes

You cannot copy the GUIDE-generated code into your class file, unless you make Aview a static method and replace all callbacks that say Aview with A.Aview.

It's easiest to simply leave the GUIDE-generated code in it's own file.