help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Function handle with arguments for GUI callback


From: Michael Goffioul
Subject: Re: Function handle with arguments for GUI callback
Date: Mon, 11 Mar 2013 14:31:04 -0400

On Mon, Mar 11, 2013 at 2:13 PM, Arnaud Miege <address@hidden> wrote:

On 11 March 2013 17:34, Michael Goffioul <address@hidden> wrote:


Using a cell array is the right approach. It is supported. What error do you get and what code are you using? Also please note that a GUI callback always get the object handle and an event data as first 2 arguments. Whatever arguments you specify in the cell array starts at position 3 in the callback argument list.

Michael.


Thanks, I have made some progress since my initial email.

In my main function, I create a variable called "gui" as the output of a sub-function called create_interface:

gui = create_interface;

In the sub-function create_interface, I add various fields to the gui structure which are handles to the various UI components, such as:

% Add pop-up menus for dimension choice of each input
strings = {'Nominal dimensions|Measurement file'};
width = [0.5 0.5 0.5];
gui.dim_choices = cell(size(gui.inputs_txt));
for k=1:length(gui.inputs_txt)
gui.dim_choices{k} = uicontrol('Style','popupmenu','String',strings,...
        'Units','normalized','Position',[0.23 y_pos(k) width(k) 0.1],...
        'BackgroundColor','w','Parent',gui.inputs_panel,'Callback',...
address@hidden,num2str(k),gui});
end

I have worked out that the first argument to the function handle callback needs to be converted to a string. I am also passing the gui structure as a second argument because I couldn't figure out a way of using the second default argument (eventdata - there doesn't seem to be much documentation about it).

This is standard Matlab behavior:
http://www.mathworks.com/help/matlab/creating_guis/writing-code-for-callbacks.html
(look for  "Use Cell Arrays with Strings")

 
However, if I start using the index k (which has a sensible value between 1 and 3), as in:

function update_browse_btn(src,data,k,gui)
% Called when user activates popup menu 
disp(['k = ' num2str(k)]);
        val = get(src,'Value')
gui.browse_btns(k,1)
end

I get the following error:

>> k = 1
val =  2
error: update_browse_btn: A(I,J): row index out of bounds; value 49 out of bound
 3
error: called from:
error:   <path_to_my_file> at line 185, colu
mn 3

I can't then close the GUI, I need to click on the console to activate it, which triggers the following error message:

error: octave_base_value::convert_to_str_internal (): wrong type argument `<unkn
own type>'
error: octave_base_value::convert_to_str_internal (): wrong type argument `<unkn
own type>'
error: octave_base_value::convert_to_str_internal (): wrong type argument `<unkn
own type>'
error: octave_base_value::double_value (): wrong type argument `<unknown type>'
error: octave_base_value::convert_to_str_internal (): wrong type argument `<unkn
own type>'
error: octave_base_value::convert_to_str_internal (): wrong type argument `<unkn
own type>'
error: octave_base_value::convert_to_str_internal (): wrong type argument `<unkn
own type>'

I then need to press Enter to regain control and close the figure with the close command.

The syntax gui.browse_btns{k} results in a similar behaviour.

As the object is a cell array, you need to index it with {}, but from what you saying, it doesn't work either. Does it change anything if you do an assignment instead:

x = gui.browse_btns{k}
x

Michael.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]