我现在正在尝试扩展Tasos在回调函数抛出意外的“不一致参数”错误中给出的答案。我想以按钮的形式再插入两个 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”语句之外,还有其他方法可以根据按钮选择来绘制图表吗?
我设法通过用弹出菜单 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