简体   繁体   中英

How to manually convert Matlab GUIDE GUI code to Octave UI Components

How should I go about to convert (manually) code created by Matlab GUIDE GUI, to use Octave's UI Components?

Stuff like this:

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
               'gui_Singleton',  gui_Singleton, ...
               'gui_OpeningFcn', @Mat_to_Octave_OpeningFcn, ...
               'gui_OutputFcn',  @Mat_to_Octave_OutputFcn, ...
               'gui_LayoutFcn',  [] , ...
               'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

etc. etc.

  1. Where can I find a comprehensive list of elements that I need to replace?

  2. How do I create the equivalent of the GUIDE callback function for Octave UI elements and can I make a single callback function for all the elements? Perhaps there's a difference here between the two.

  3. Is the GUID GUI code open, or open source? It surely seems so here .

  4. Is the UI layout that was set by the user in the GUIDE accessible? ie Do we know where the button's width and text color settings are saved?

  5. Is there a comprehensive list somewhere? If so, where? Are all or at least most of the elements from GUIDE available in UI Components? How can I check this?

  6. Is there anything I left out before starting the task?

For the most part, GUI creation on octave is identical to matlab. GUI-creation is a relatively new addition to octave, so expect a couple of the more recent additions in the matlab family to have not yet made it into octave, but for the most part, matlab code implementing a GUI application should work on octave with no or very little need for tweaking. Here are the respective manual entries for matlab and octave ; you will notice that the core functions are identical.

One important 'catch' is that octave does not support handles to nested functions for the time being (this might change later). For example, consider the following matlab code implementing a simple GUI with a slider affecting a plot (taken from this answer ).

%%%%%% In file myplot.m %%%%%
function myplot

  %% Create initial figure and spiral plot
  figure;  axes ('position', [0.1, 0.3, 0.8, 0.6]);
  t = linspace (0, 8*pi, 100);  x = t .* cos(t);  y = t .* sin(t);
  plot (x, y);  axis ([-100, 100, -100, 100]);

  %% Add ui 'slider' element      
  hslider = uicontrol (                    ...
         'style', 'slider',                ...
         'Units', 'normalized',            ...
         'position', [0.1, 0.1, 0.8, 0.1], ...
         'min', 1,                         ...
         'max', 50,                        ...
         'value', 10,                      ...
         'callback', {@plotstuff}          ...
       );

  %% Callback function called by slider event
  function plotstuff (h, event)
    n = get (h, 'value');
    x = n * t .* cos(t);  y = n * t .* sin(t);
    plot (x, y);  axis ([-100, 100, -100, 100]);
  end
end

If you try running this on octave, you will get the following error message:

>> myplot
error: handles to nested functions are not yet supported
error: called from myplot at line 10 column 11

Converting the nested function into an independent function or subfunction resolves this (as demonstrated in this answer ).

As for GUIDE, while octave does not have a similar "user-friendly" graphical tool for GUI-app creation yet, at the end of the day, all GUIDE does is produce appropriate underlying code for UI-element creation, which in theory should be compatible with octave. Having said that, it's worth reading up exactly what files GUIDE creates, namely a '.fig' file loading up the figure element, and a 'functions' file holding callbacks and actual code, etc. So, "running" a GUIDE generated file in octave will probably involve 'loading' the figure first. Also, in practice, GUIDE may make use of nested functions for callbacks, so the code might take a bit of tweaking to convert these into suitable subfunctions to get it working on octave.

Having said that, GUIDE really is more for people who like to avoid 'actual' code, but in fact, coding the GUI in matlab / octave directly is probably far more straightforward, once you get familiar with how get / set commands work for manipulating ui-element properties. And if you're after GUI solutions that work for both octave and matlab, I would certainly advise going down this route, and sticking to subfunctions instead of nested functions.

To answer the remaining two questions which haven't been covered by the above:

  • No, GUIDE is not open source (let alone free software). It is proprietary code from Mathworks which uses their licence. In particular, in theory there might be licencing issues with using GUIDE-generated code with octave, but I don't know for sure.

  • GUIDE generates a .fig file directly. This is a binary file that can be loaded onto matlab (and in theory, octave). With GUIDE, there is no other 'source' file detailing the uielements and their properties that were used to create this figure. Having said that, in matlab, once the figure is loaded, you can export 'source code' from the figure's graphical menu, that re-creates this figure, if desired. However, this may not be the most human-friendly code to look at. This is one of the reasons to prefer the programmatic approach over GUIDE: you have clean, clear source code, which details the properties of the uielements programmatically, rather than having to fish them out by loading a figure and searching through its properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM