如何手动将Matlab GUIDE GUI代码转换为Octave UI组件 [英] How to manually convert Matlab GUIDE GUI code to Octave UI Components

查看:166
本文介绍了如何手动将Matlab GUIDE GUI代码转换为Octave UI组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何转换(手动)由Matlab GUIDE GUI创建的代码,以使用Octave的UI组件?

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

像这样的东西:

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

等等等

  1. 在哪里可以找到需要替换的元素的完整列表?

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

如何为八度UI元素,我可以为所有元素制作一个回调函数吗?也许两者之间是有区别的.

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.

GUID GUI代码是打开的还是开源的?它肯定在这里看起来如此 .

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

是否可以访问用户在GUIDE中设置的UI布局?即我们知道按钮的宽度和文本颜色设置保存在哪里吗?

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

某处是否有详尽的清单?如果是这样,在哪里? UI组件中的GUIDE的全部或至少大多数元素是否可用?我该如何检查?

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?

在开始任务之前我还有什么遗漏吗?

Is there anything I left out before starting the task?

推荐答案

在大多数情况下,在八度音阶上的GUI创建与matlab相同. GUI创建是octave的一个相对较新的新增功能,因此,请期待matlab系列中的两个较新的附加功能尚未将其添加到octave中,但是在大多数情况下,实现GUI应用程序的matlab代码应在octave上工作.无需调整或调整的需求很少.以下是 matlab 八度;您会注意到核心功能是相同的.

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.

一个重要的'catch'是八度暂时不支持嵌套函数的句柄(稍后可能会更改).例如,考虑下面的matlab代码,它使用一个影响图的滑块来实现一个简单的GUI(摘自此答案).

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
错误:尚不支持嵌套函数的句柄
错误:来自 myplot在第10行的第11列

>> 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).

对于GUIDE,虽然octave尚不具有用于GUI-app创建的类似用户友好"图形工具,但归根结底,所有GUIDE所做的都是为UI元素创建生成适当的基础代码,该代码理论上应该与八度兼容.话虽如此,但值得一读的是GUIDE创建的文件,即加载了Figure元素的".fig"文件,以及包含回调和实际代码的"functions"文件.因此,运行" GUIDE生成的文件以八度为单位可能需要先加载"图形.另外,在实践中,GUIDE可能将嵌套函数用于回调,因此代码可能需要进行一些调整才能将它们转换为合适的子函数,以使其在八度音阶下工作.

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.

话虽如此,GUIDE确实更适合希望避免使用实际"代码的人,但是实际上,一旦您熟悉get/set命令用于操纵ui元素属性.而且,如果您追求适用于octave和matlab的GUI解决方案,我当然会建议您走这条路线,并坚持使用子功能而不是嵌套功能.

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:

  • 否,GUIDE不是开源的(更不用说免费软件了).它是Mathworks的专有代码,使用它们的许可证.特别是,从理论上讲,将GUIDE生成的代码与八度一起使用可能会出现许可问题,但我不确定.

  • 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直接生成一个.fig文件.这是一个二进制文件,可以加载到matlab上(理论上是八度).使用GUIDE,没有其他源"文件详细说明用于创建该图形的uielements及其属性.话虽如此,在Matlab中,一旦加载了图形,就可以从图形的图形菜单中导出源代码",如果需要,可以重新创建该图形.但是,这可能不是最人性化的代码.这是与GUIDE相比更喜欢编程方法的原因之一:您拥有干净,清晰的源代码,该代码以编程方式详细说明了uielements的属性,而不必通过加载图形并对其属性进行搜索来找出它们.

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.

这篇关于如何手动将Matlab GUIDE GUI代码转换为Octave UI组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆