Octave uicontrol 按钮元素
Posted
技术标签:
【中文标题】Octave uicontrol 按钮元素【英文标题】:Octave uicontrol button elements 【发布时间】:2021-05-26 07:21:42 【问题描述】:我现在正在尝试扩展 Tasos 在callback function throws unexpected "nonconformant arguments" error 中给出的答案。我想以按钮的形式再插入两个 uicontrol。我想要两个按钮来控制两个功能中的哪一个将显示在图表上(连同之前定义的滑块)。如果按下第一个按钮 (elp_button),则应绘制函数 Eu 和 Ev,而如果按下第二个按钮 (pot_button),则应绘制函数 Pu 和 Pv。但是,我得到一个空图。这是我制作的脚本。
%1
%Definiramo klizace ispod grafa
function radiodugme()
% Oznaka za dugmad
rd_label = uicontrol (
"style", "text",
"units", "normalized",
"string", "Prikaz:",
"horizontalalignment", "left",
"position", [0.05 0.16 0.05 0.05],
"fontsize", 16 );
% Dugme za el. polje
elp_button = uicontrol (
"style", "radiobutton",
"units", "normalized",
"string", "El. polje",
"position", [0.05 0.09 0.05 0.05],
"fontsize", 16 );
% Dugme za el. pot.
pot_button = uicontrol (
"style", "radiobutton",
"units", "normalized",
"string", "El. pot.",
"position", [0.05 0.02 0.05 0.05],
"fontsize", 16 );
% Oznaka i klizac za R
R_label = uicontrol(
"style", "text",
"units", "normalized",
"position", [0.20, 0.02, 0.10, 0.05],
"string", "R",
"fontsize", 16 );
R_slider = uicontrol(
"style", "slider",
"units", "normalized",
"position", [0.35, 0.035, 0.50, 0.1],
"min", 0.01,
"max", 0.05 );
% Oznaka i klizac za rho
rho_label = uicontrol(
"style", "text",
"units", "normalized",
"position", [0.20, 0.09, 0.10, 0.05],
"string", "rho (C/m3)",
"fontsize", 16 );
rho_slider = uicontrol(
"style", "slider",
"units", "normalized",
"position", [0.35, 0.105, 0.50, 0.1],
"min", 0.1,
"max", 1 );
% Oznaka i klizac za er
er_label = uicontrol(
"style", "text",
"units", "normalized",
"position", [0.20, 0.16, 0.10, 0.05],
"string", "e_r",
"fontsize", 16 );
er_slider = uicontrol(
"style", "slider",
"units", "normalized",
"position", [0.35, 0.175, 0.50, 0.1],
"min", 1,
"max", 15 );
% Pozivamo callback funkciju, postavimo pocetne klizace i graf
R_init = 0.01;
rho_init = 0.1;
er_init = 1;
set (elp_button,
"value", 1,
"callback", @rd_callback, elp_button, pot_button );
set (pot_button,
"value", 0,
"callback", @rd_callback, elp_button, pot_button );
set( R_slider,
"value", R_init,
"callback", @slider_callback, R_slider, rho_slider, er_slider );
set( rho_slider,
"value", rho_init ,
"callback", @slider_callback, R_slider, rho_slider, er_slider );
set( er_slider,
"value", er_init ,
"callback", @slider_callback, R_slider, rho_slider, er_slider );
plot_elppot ( R_init, rho_init, er_init );
endfunction
% Definiramo callback funkciju - poziva radiodugme i klizace
function rd_callback (active_handle, event, elp_button, pot_button )
slider_callback (active_handle, event, R_slider, rho_slider, er_slider );
endfunction
% Definiramo callback funkciju - poziva klizace i crtanje grafa
function slider_callback (active_handle, event, R_slider, rho_slider, er_slider )
R = get ( R_slider, "value" );
rho = get ( rho_slider, "value" );
er = get ( er_slider, "value" );
plot_elppot ( R, rho, er );
endfunction
% Definiramo funkciju za crtanje grafa
function plot_elppot ( R, rho, er )
e0 = 8.8541878128e-12;
rv = R : 0.001 : 0.1;
Ev = (R^3 * rho)./(3 * e0 * rv.^2);
Pv = (R^3 * rho)./(3 * e0 * rv);
ru = 0 : 0.001 : R;
Eu = (rho * ru)./(3 * e0 * er);
Pu = (rho * ( R^2 * (2*er + 1) - ru.^2 ))./(6 * e0 * er);
%Crtamo graf Eu/Ev ili Pu/Pv
axes ("position", [0.2, 0.325, 0.675, 0.6]);
if get ( elp_button, "value" )
set (pot_button, "value", 0);
plot (
ru, Eu, "linewidth", 2.5,
rv, Ev, "linewidth", 2.5 );
set( gca,
"xlim", [0, 0.1], "ylim", [0, 2d9],
"xlabel", "r (m)", "ylabel", "E (V/m)",
"fontsize", 18,
"xgrid", "on", "ygrid", "on" );
endif
if get ( pot_button, "value" )
set (elp_button, "value", 0);
plot (
ru, Pu, "linewidth", 2.5,
rv, Pv, "linewidth", 2.5 );
set( gca,
"xlim", [0, 0.1], "ylim", [0, 1.6d8],
"xlabel", "r (m)", "ylabel", "\\phi (V)",
"fontsize", 18,
"xgrid", "on", "ygrid", "on" );
endif
%Legenda
l = legend( sprintf(
" R = %.2f,\n \\rho = %.2f,\n \\epsilon_r = %.2f", R, rho, er ) );
set( l,
"fontsize", 18,
"location", "northwestoutside" );
endfunction
当 Octave 尝试绘制函数时,似乎出现了问题。我使用“if”语句来确定按下了哪个按钮。这可能是问题吗?我尝试按照https://wiki.octave.org/Uicontrols 的示例进行操作,但我无法理解将其转移到我的示例中。除了使用“if”语句之外,还有其他方法可以根据按钮选择来绘制图表吗?
提前致谢。
最好的问候,
伊戈尔
【问题讨论】:
【参考方案1】:我设法通过将单选按钮 uicontrol 替换为弹出菜单 uicontrol 来解决问题。现在的代码如下所示:
%1
%Definiramo izbornik i klizace ispod grafa
function iamenu()
% Oznaka za izbornik
elpp_label = uicontrol (
"style", "text",
"units", "normalized",
"string", " Prikaz:",
"horizontalalignment", "left",
"position", [0.05 0.16 0.05 0.05],
"fontsize", 16 );
% Izbornik za el. polje i pot.
elpp_popup = uicontrol (
"style", "popupmenu",
"units", "normalized",
"string", "El. polje", "Potencijal",
% "callback", @update_plot
"position", [0.05 0.09 0.1 0.05],
"fontsize", 16 );
% Oznaka i klizac za R
R_label = uicontrol(
"style", "text",
"units", "normalized",
"position", [0.20, 0.02, 0.10, 0.05],
"string", "R",
"fontsize", 16 );
R_slider = uicontrol(
"style", "slider",
"units", "normalized",
"position", [0.35, 0.035, 0.50, 0.1],
"min", 0.01,
"max", 0.05 );
% Oznaka i klizac za rho
rho_label = uicontrol(
"style", "text",
"units", "normalized",
"position", [0.20, 0.09, 0.10, 0.05],
"string", "rho (C/m3)",
"fontsize", 16 );
rho_slider = uicontrol(
"style", "slider",
"units", "normalized",
"position", [0.35, 0.105, 0.50, 0.1],
"min", 0.1,
"max", 1 );
% Oznaka i klizac za er
er_label = uicontrol(
"style", "text",
"units", "normalized",
"position", [0.20, 0.16, 0.10, 0.05],
"string", "e_r",
"fontsize", 16 );
er_slider = uicontrol(
"style", "slider",
"units", "normalized",
"position", [0.35, 0.175, 0.50, 0.1],
"min", 1,
"max", 15 );
% Postavimo pocetni izbornik, klizace i graf, te pozivamo callback funkciju plot_elppot
iaelpp_init = 1;
R_init = 0.01;
rho_init = 0.1;
er_init = 1;
set (elpp_popup,
"value", iaelpp_init,
"callback", @iamenu_callback, elpp_popup, R_slider, rho_slider, er_slider );
set( R_slider,
"value", R_init,
"callback", @iamenu_callback, elpp_popup, R_slider, rho_slider, er_slider );
set( rho_slider,
"value", rho_init ,
"callback", @iamenu_callback, elpp_popup, R_slider, rho_slider, er_slider );
set( er_slider,
"value", er_init ,
"callback", @iamenu_callback, elpp_popup, R_slider, rho_slider, er_slider );
plot_elppot ( iaelpp_init, R_init, rho_init, er_init );
endfunction
% Definiramo callback funkciju - poziva izbornik i klizace te crtanje grafa s plot_elppot
function iamenu_callback (active_handle, event, elpp_popup, R_slider, rho_slider, er_slider )
iaelpp = get ( elpp_popup, "value");
R = get ( R_slider, "value" );
rho = get ( rho_slider, "value" );
er = get ( er_slider, "value" );
plot_elppot ( iaelpp, R, rho, er );
endfunction
% Definiramo funkciju za crtanje grafa
function plot_elppot ( iaelpp, R, rho, er )
e0 = 8.8541878128e-12;
rv = R : 0.001 : 0.1;
Ev = (R^3 * rho)./(3 * e0 * rv.^2);
Pv = (R^3 * rho)./(3 * e0 * rv);
ru = 0 : 0.001 : R;
Eu = (rho * ru)./(3 * e0 * er);
Pu = (rho * ( R^2 * (2*er + 1) - ru.^2 ))./(6 * e0 * er);
%Crtamo graf Eu/Ev ili Pu/Pv, ovisno o izboru iaelpp
axes ("position", [0.2, 0.325, 0.675, 0.6]);
if ( iaelpp == 1 )
plot (
ru, Eu, "linewidth", 2.5,
rv, Ev, "linewidth", 2.5 );
set( gca,
"xlim", [0, 0.1], "ylim", [0, 2d9],
"xlabel", "r (m)", "ylabel", "E (V/m)",
"fontsize", 18,
"xgrid", "on", "ygrid", "on" );
endif
if ( iaelpp == 2 )
plot (
ru, Pu, "linewidth", 2.5,
rv, Pv, "linewidth", 2.5 );
set( gca,
"xlim", [0, 0.1], "ylim", [0, 1.6d8],
"xlabel", "r (m)", "ylabel", "\\phi (V)",
"fontsize", 18,
"xgrid", "on", "ygrid", "on" );
endif
%Legenda
l = legend( sprintf(
" R = %.2f,\n \\rho = %.2f,\n \\epsilon_r = %.2f", R, rho, er ) );
set( l,
"fontsize", 18,
"location", "northwestoutside" );
endfunction
但是现在出现了另一个问题。当从弹出菜单中选择第二个选项时,绘图会很好地显示出来,但它会覆盖在第一个选项的绘图上。似乎轴和情节都被保留了。我尝试在 if 语句中使用 cla、clear、refresh、drawow、delete 和 hold 命令,但没有一个是令人满意的。我想在选择另一个弹出菜单选项时同时替换坐标轴和绘图。如果有人知道如何解决这个问题,我将不胜感激。
最好的问候,
伊戈尔
【讨论】:
以上是关于Octave uicontrol 按钮元素的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法使用 TeX(希腊字母、下标、上标等)使 octave 解释 uicontrol 静态文本中的字符串?
Swift:UIScrollView 和 UIControl - addTarget | addGestureRecognizer - 没有按预期工作