I'm trying now to expand the answer given by Tasos in callback function throws unexpected "nonconformant arguments" error. I would like to insert two more uicontrols in the form of buttons. I'd like to have two buttons which regulate which of the two functions will be shown on the graph (together with the sliders defined previously). If the first button (elp_button) is pressed, functions Eu and Ev should be plotted, while if the second button (pot_button) is pressed, functions Pu and Pv should be plotted. However, I get an empty graph. Here's the script I made.
%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
It seems that the problem arises when Octave tries to plot the functions. I used the 'if' statements to determine which button is pressed. Could that be the issue? I tried following the example at https://wiki.octave.org/Uicontrols, but I cannot understand it enough to transfer it to my example. Is there another way to plot the graphs depending on the button choice, except using the 'if' statement?
I managed to solve the problem by replacing the radio button uicontrol with the popup menu uicontrol. The code now looks like this: