Question

With Guide I made a Matlab Gui that have 10 Axes in which i want to display images, in all of them at once, after i press a Button.

I made a separate .m file with function Load_Write_Img_Results(img_index) that i call from the Button Callback which have the following code:

for i = 1 : 10
    handles_imgOut = findobj('Tag', ['imgOut' num2str(i)]);
    set(handles_imgOut, 'HandleVisibility', 'ON');
    axes(handles_imgOut);
    image(imgs_data{img_index(i)});
    ...
end

Every time when i run the main Gui and press the button for the first time, images are displayed in all axes, so everything works ok.


The problem appear when i press the button for the second time and i get this error:

    Error using axes  
    Invalid object handle

on this line:

    axes(handles_imgOut);

When debugging, i saw that after handles_imgOut = findobj('Tag', ['imgOut' num2str(i)]); the handles_imgOut is not getting any value and is empty, so it's obvious that error.

Is there any chance i can't get handles for axes at the second press of the button?

Also, i want to know how can i solve this warning from Matlab:

Calling AXES(h) in a loop can be slow. Consider moving the call to AXES outside the loop.

Thanks in advance, any suggestions are welcome!


[SOLUTION]:

for i = 1 : 10
    handles_imgOut = findobj('Tag', ['imgOut' num2str(i)]);
    set(handles_imgOut, 'HandleVisibility', 'ON');
    axes(handles_imgOut);
    image(imgs_data{img_index(i)});

    set(gca, 'Tag', ['imgOut' num2str(i)]); //! renew the tag
    ...
end
Was it helpful?

Solution

The fact that it works on the first button press, but not thereafter hints that

image(imgs_data{img_index(i)});

opens new axes instead of drawing into the existing ones. Since the new axes are not initialized with your tags, findobj will not find them. Either

(1) make sure to hold the original axes by placing a hold on command right after their creation,

or

(2) renew the tag right after the image command by

set(gca, 'Tag', ['imgOut' num2str(i)]);

OTHER TIPS

I'm fairly new to GUIDE and I've encountered similar problems, with the graph not being updated more than once / axes not found. In the end, I used the following approach, which I hope can be useful to you too:

    % I get my axes directly from the handles object, instead of using findObj:
        graph1 = handles.axes1;
        graph2 = handles.axes2;
    % clear axes
        cla(graph1);
        cla(graph2);
    % showTrial is my function that draws the graphs - 
    %notice that I used handles to store other variables as well (.data, .trials)
    showTrial(handles.data, handles.trials(1), graph1, graph2)

To sum it up:

  • don't use findObj, get your axes from handles (it should automatically contain them as imgOut1,imgOut2, etc.)

  • pass the axes to your drawing function or pass directly the handles variable e.g. Load_Write_Img_Results(img_index, handles)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top