OctaveでGraphical User Interface(GUI)を使う

Octave7.3(Deb12)/9.6(w11)で検証

GUIを用いた読み込みと書き込みファイルの指定

ファイルを選ぶ

ファイル名が決まっていない場合や任意のファイルを選びたい場合などでは、 OctaveではGUIを用いて選んだファイルのファイル名を取得できます。

  • ファイルを開く

./fig_octave_gui/open_file.png

1[fname fpath fltindex]=uigetfile();

Octaveを実行したフォルダを開きます。

別のフォルダを指定する場合は、右辺()内に"folderpath"を記します。

[fname fpath fltindex]=uigetfile("c:\\data\\*.ext")とすれば、 (i)フォルダc:dataにある、 (ii)拡張子extをもつファイル、 を表示します。 \は円記号です。

  • ファイルを保存する

./fig_octave_gui/save_file.png

1     [fname fpath fltindex]=uiputfile();
  • 戻り値fname,fpathは、それぞれファイル名とパス(フォルダ名)です。

読み書きするためのパスまで含んだ全ファイル名は次のようにすれば設定できます。

1FILENAME=[fpath fname]
  • uigetdirもあります

ダイアログBOXを使う

簡単なGUI使用例を紹介します。

windowsのOctaveではmsgboxに日本語(2バイト文字)は表示できないようです。(Linuxでは表示可)

  1. メッセージ表示
./fig_octave_gui/msgbox.png./fig_octave_gui/err_dlg.png./fig_octave_gui/help.png./fig_octave_gui/warning.png

左から順に以下のとおりです。

1H=msgbox("OK!?","TITLE");
2uiwait(H)
1H=errordlg("ERROR!","DANGER!");
2uiwait(H)
1H=helpdlg("HELP YOU","MayI");
2uiwait(H)
1H=warndlg("WARNING!!!","Caution");
2uiwait(H)

uiwait(H)がないと、OKボタンを押さなくてもプログラム実行は次に進んでいきます。

  1. Yes/No入力

./fig_octave_gui/y_n_dialog.png

1btn = questdlg ("Close Octave?", "Some title", "Yes", "No", "No");
2
3if (strcmp (btn, "Yes"))
4  exit ();
5else
6  H=msgbox("Octave continue");
7  uiwait(H)
8endif

詳細を見たいときはhelp questdlgまたは、doc questdlgしてください。

  • 基本は quetdlg("メッセージ","タイトル") で、Yes,No,Cancelボタンが現れます。
  • Yes/Noの表示を変えたいときは、以下のようにしてください。

    1quetdlg("メッセージ","タイトル","ボタン1の表示名","ボタン2の表示名","defaultのボタン")

    defaultのボタン指定はなくても構いません。

    また、指定できるボタンは2つまでのようです。

  • 押されたボタンを検出するための文字列の比較はstrcmpで行います。
  1. 入力ボックス サンプルソースを紹介します。 実行して、内容を理解してください。

./fig_octave_gui/input_dlg.png

1prompt = {"Width", "Height", "Depth"};
2defaults = {"1.10", "2.20", "3.30"};
3rowscols = [1,10; 2,20; 3,30];
4dims = inputdlg (prompt, "Enter Box Dimensions", ...
5        rowscols, defaults);
  • 実行したらcell配列dimsの中に入力値が保存されています。 セル配列の扱いを参考に、取り扱ってください。

マウスクリック座標を取得する

octaveのfigure画面上をマウスクリックしたときの座標を取得できます。

[x y buttons]=ginput(n)

nはイベント回数,n=1とすれば1回発生時の座標取得、nの定義がなければ、ret が押されて終了するまでの座標をすべて記憶しています。

もっとGUI

UI Elementには、 uifigure(), uipanel(), uibuttongroup(), uicontrol(), uitable(), uimenu(), uicontextmenu(), uitoolbar(), uipushtool(), uitoggletool() などなどあるようですが、uicontrol()だけでもそれなりになるようです。

uicontrol()

以下のguiがあります。

"checkbox" "edit" "listbox" "popupmenu" "pushbutton" "radiobutton" "slider" "text" "togglebutton"

  • figure画面にguiのパーツを置く
  • イベントはcallbackで指定した関数に飛ばして処理する
  • guidata(H,DATA);%GUIdataをfigure handle Hにstoreする

GUIとサンプルソースを示します。参考にしてください。

./fig_octave_gui/gui_sample.png

github/oct_gui_sample.m

  1clear all
  2close all
  3clc
  4
  5
  6function mylist_callback(hs,evt, arg1)
  7    msgbox(get(hs,"string"){get(hs,"value")},"ListboxSelection");
  8end
  9
 10function mybutton_grb(hs,evt, arg1)
 11    msgbox(["called from grb " num2str(arg1)],"group button");
 12end
 13
 14function update_menu(obj, init=false)
 15
 16    hs=guidata(obj);
 17    %gcbo holds the habdle of the control
 18    switch(gcbo)
 19      case {hs.gb1}
 20        if((get(hs.gb1,"value")==true) && (get(hs.gb2,"value")==false))
 21           msgbox("Choice1","Selected");
 22        end
 23      case {hs.gb2}
 24        if((get(hs.gb2,"value")==true) && (get(hs.gb1,"value")==false))
 25           msgbox("Choice2","Selected");
 26        end
 27      case {hs.tglbtn1}% 0/1変化
 28        num=get(gcbo, "value");
 29        disp(num)
 30      case {hs.ed1}
 31        ed_gui=get(gcbo,"string");
 32        disp(ed_gui);
 33      case {hs.cb1}
 34        cb1_gui=get(gcbo,"value");
 35        disp(cb1_gui);
 36      case {hs.slb1}
 37        slb1_num_gui=get(gcbo,"value");
 38        set(hs.slb1_num,"string",num2str(slb1_num_gui));
 39    end
 40end
 41
 42
 43%## Create figure and panel on it
 44h.f=figure("position",[100 100 640 480]);
 45
 46h.p = uipanel ("title", "Panel Title Menu",
 47               "units", "normalized",
 48               "position", [.4 .25 .3 .2]);
 49
 50%## add two buttons to the panel
 51h.b1 = uicontrol ("parent", h.p,
 52                  "string", "A Button", 
 53                  "units", "normalized",
 54                  "horizontalalignment", "left",
 55                  "position", [.1 .6 .8 .3],
 56                  "callback", @update_menu);
 57
 58h.b2 = uicontrol ("parent", h.p,
 59                  "string", "Another Button", 
 60                  "units", "normalized",
 61                  "horizontalalignment", "left",
 62                  "position",[.1 .2 .8 .3],
 63                  "callback", @update_menu);
 64
 65%## Create a button group
 66h.gp = uibuttongroup ("parent",h.f,
 67                      "units", "normalized",
 68                      "Position", [0 0.8 1 0.2]);
 69%## Create a buttons in the group
 70h.gb1 = uicontrol ("parent", h.gp,
 71                   "style", "radiobutton", 
 72                   "string", "Choice 1",
 73                   "value",false,
 74                   "units", "normalized",
 75                   "horizontalalignment", "left",
 76                   "position", [ .1 .6 .2 .2 ],
 77                   "callback", @update_menu);
 78h.gb2 = uicontrol ("parent", h.gp,
 79                   "style", "radiobutton",
 80                   "string", "Choice 2", 
 81                   "value",false,
 82                   "units", "normalized",
 83                   "horizontalalignment", "left",
 84                   "position", [ .1 .2 .2 .2 ],
 85                   "callback",@update_menu);
 86
 87h.gp2 = uibuttongroup ("parent", h.f,
 88                       "title","grb",
 89                       "units", "normalized",
 90                       "Position", [ 0.4 0.5 0.3 0.15] );
 91
 92h.grb1 = uicontrol ("parent",h.gp2,
 93                    "style", "radiobutton",
 94                    "string", "Choice grb1",
 95                    "units", "normalized",
 96                    "position", [ .1 .6 .7 .3 ],
 97                    "callback",{@mybutton_grb,1});
 98
 99h.grb2 = uicontrol ("parent", h.gp2,
100                    "style", "radiobutton",
101                    "string", "Choice grb2",
102                    "units", "normalized",
103                    "horizontalalignment", "left",
104                    "position", [ .1 .2 .7 .3 ],
105                    "callback",{@mybutton_grb,2});
106
107%## Create a button not in the group
108h.rb0 = uicontrol ("parent",h.f,
109                   "style", "radiobutton",
110                   "string", "Not in the group", 
111                   "units", "normalized",
112                   "horizontalalignment", "left",
113                   "position", [ .1 .5 .2 .2 ],
114                   "callback", @update_menu);
115
116%## Create an edit control
117h.ed1 = uicontrol ("parent", h.f,
118                  "style", "edit",
119                  "string", "editable text", 
120                  "units", "normalized",
121                  "horizontalalignment", "left",
122		  "position", [.1 .4 .2 .1],
123                  "callback", @update_menu );
124
125%## Create a checkbox
126h.cb1 = uicontrol ("parent", h.f,
127                  "style", "checkbox",
128                  "string", "a checkbox", 
129                  "units", "normalized",
130                  "horizontalalignment", "left",
131		  "position", [.1 .2 .15 .1],
132                  "callback", @update_menu);
133
134mylist={"hoge1","hoge2","hoge3","hoge4","hoge5"};
135h.lb1 = uicontrol ("parent", h.f,
136                   "style", "listbox",
137                   "string", mylist,
138                   "units", "normalized",
139                   "horizontalalignment", "left",
140                   "position", [.1 0.1 .2 .1],
141                   "callback", {@mylist_callback, mylist});
142
143h.cont_list = uicontrol ("parent", h.f,
144%                         "title","select controller",
145                         "style", "popupmenu",
146                         "units", "normalized",
147                         "string", {"none",
148                                    "PID",
149                                    "PoleSet",
150                                    "LQ",
151                                    "Fuzzy",
152                                    "STR",
153                                    "SlidingMode",
154                                    "BackStepping",
155                                    "SimpleAdaptiveCont.",
156                                    "ModelPredictCont."},
157                         "horizontalalignment", "left",
158                         "position", [0.6 0.04 0.2 0.1],
159                         "callback", @update_menu);
160
161h.l_label = uicontrol ("parent",h.f,
162                       "style", "text",
163                       "units", "normalized",
164                       "string", "SelectContoller",
165                       "horizontalalignment", "left",
166                       "position", [0.6 0.15 0.15 0.05]);
167
168%two states,0/1
169h.tglbtn1 = uicontrol ("parent",h.f,
170                       "style", "togglebutton",
171                       "units", "normalized",
172                       "string", "tglbtn",
173                       "horizontalalignment", "left",
174                       "position", [0.15 0.3 0.1 0.05],
175                       "callback", @update_menu);
176
177slb_ini=0.4;
178h.slb1 = uicontrol ("parent",h.f,
179                    "style", "slider",
180                    "units", "normalized",
181                    "value", slb_ini, % 初期値の指定,max/min指定なければ変化幅は0/1
182                    "max",10,
183                    "min",-100,
184                    "sliderstep",[0.001 0.01],
185                    "horizontalalignment", "left",
186                    "Position", [ 0.1 0.7 0.3 0.05],
187                    "callback",@update_menu);
188
189h.slb1_num=uicontrol("parent",h.f,
190                     "style","text",
191                    "units", "normalized",
192                     "string",num2str(slb_ini),
193                     "horizontalalignment", "left",
194                     "position", [ 0.42 0.7 0.2 0.05]);
195
196h.sl_ver = uicontrol ("parent",h.f,
197                      "style", "slider",
198                      "units", "normalized",
199                      "string", "volume",
200                      "value", 0.4, % 初期値の指定,出力は0/1
201                      "horizontalalignment", "left",
202                      "Position", [ 0.8 0.4 0.05 0.3],%幅/高さ比で自動的に向きが変わる!
203                      "callback",@update_menu);
204
205set (h.f, "color", get(0, "defaultuicontrolbackgroundcolor"));
206guidata(h.f, h);
207update_menu(h.f, true);

参考サイト

このシリーズの投稿