简体   繁体   English

如何手动将Matlab GUIDE GUI代码转换为Octave UI组件

[英]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? 我应该如何转换(手动)由Matlab GUIDE GUI创建的代码,以使用Octave的UI组件?

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? 如何为Octave UI元素创建等效的GUIDE回调函数,是否可以为所有元素创建单个回调函数? Perhaps there's a difference here between the two. 也许这两者之间存在差异。

  3. Is the GUID GUI code open, or open source? GUID GUI代码是打开还是开源? It surely seems so here . 肯定是这样的

  4. Is the UI layout that was set by the user in the GUIDE accessible? 是否可以访问GUIDE中用户设置的UI布局? 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? UI组件中是否提供了GUIDE中的所有元素或至少大部分元素? 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创建与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. GUI创建是八度音阶的一个相对较新的补充,所以期望matlab系列中的几个最近的添加尚未使它成为八度音阶,但是在大多数情况下,实现GUI应用程序的matlab代码应该在八度音程上工作没有或很少需要调整。 Here are the respective manual entries for matlab and octave ; 以下是matlaboctave的相应手册条目; 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). 一个重要的“捕获”是octave暂时不支持嵌套函数的句柄(这可能会在以后发生变化)。 For example, consider the following matlab code implementing a simple GUI with a slider affecting a plot (taken from this answer ). 例如,考虑以下matlab代码实现一个带有影响绘图的滑块的简单GUI(取自此答案 )。

%%%%%% 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 >> myplot
error: handles to nested functions are not yet supported 错误:尚不支持嵌套函数的句柄
error: called from myplot at line 10 column 11 错误:从第10行第11列的myplot调用

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. 至于GUIDE,虽然octave还没有类似的“用户友好”图形工具用于GUI-app创建,但最终,所有GUIDE都会为UI元素创建生成适当的底层代码,理论上应该兼容八度音程。 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. 话虽如此,值得准确读取GUIDE创建的文件,即加载图元素的'.fig'文件,以及保存回调和实际代码的'函数'文件等。因此,“运行”GUIDE生成的文件在八度音阶中可能会首先涉及“加载”这个数字。 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. 此外,在实践中,GUIDE可能会使用嵌套函数进行回调,因此代码可能需要进行一些调整才能将这些函数转换为合适的子函数以使其在八度音程上工作。

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. 话虽如此,GUIDE确实更适合那些喜欢避免“实际”代码的人,但事实上,一旦你熟悉了get / set命令如何用于操作,直接用matlab / octave编写GUI可能会更加直截了当ui-elements属性。 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. 如果您正在使用适用于octave和matlab的GUI解决方案,我肯定会建议沿着这条路线前进,并坚持使用子功能而不是嵌套功能。

To answer the remaining two questions which haven't been covered by the above: 回答上述未涵盖的其余两个问题:

  • No, GUIDE is not open source (let alone free software). 不,GUIDE不是开源的(更不用说免费软件了)。 It is proprietary code from Mathworks which uses their licence. 它是Mathworks的专有代码,使用其许可证。 In particular, in theory there might be licencing issues with using GUIDE-generated code with octave, but I don't know for sure. 特别是,从理论上讲,使用带有八度音程的GUIDE生成的代码可能存在许可问题,但我不确定。

  • GUIDE generates a .fig file directly. GUIDE直接生成.fig文件。 This is a binary file that can be loaded onto matlab (and in theory, octave). 这是一个二进制文件,可以加载到matlab(理论上是八度)。 With GUIDE, there is no other 'source' file detailing the uielements and their properties that were used to create this figure. 使用GUIDE,没有其他“源”文件详细说明用于创建此图的元素及其属性。 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. 话虽如此,在matlab中,一旦加载了图形,您可以从图形的图形菜单中导出“源代码”,如果需要,可以重新创建该图形。 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. 这是优于程序化方法优于GUIDE的原因之一:您拥有干净,清晰的源代码,它以编程方式详细说明了元素的属性,而不是通过加载图形并搜索其属性来将它们删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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