Question

I made plot in GUI with two different vectors on y axis and same vector on x axis on one axes.

Then I put two checkboxes in GUI.

I want to make for exemple when first checkbox is 'on' that my axes show me first vector on plot and when is 'off' that visibilty of vector on axes is off, same for second checkbox only second vector. Actually my checkboxes is used to turn on or off visibility of individual vectors on axes.

Here is my code:

function checkbox1_Callback(hObject, eventdata, handles)    
    if (get(hObject,'Value')) == 1  
        plot(vectorx,vectory);
    else 
        ???
    end

When my checkbox is 'on' vectors are visible at plot but I can't remove it when checkbox is 'off', I can with function cla() but that function remove all on axes.

Was it helpful?

Solution

If you want the checkbox to only control the visibility of a given plot, you need to create your plot before and save the handle of your plot, so you can reuse it later to switch the visibility on or off.

somewhere else in your code:

handles.handle_plot1 = plot(vectorx,vectory);

then in the callback of your checkbox:

function checkbox1_Callback(hObject, eventdata, handles)
   handles.handle_plot1 = handles.handle_plot1 %// optional, you can use another way to retrieve your handle
   if (get(hObject,'Value')) == 1  
       set(handle_plot1, 'visible' , 'on')
   else 
       set(handle_plot1, 'visible' , 'off')
   end

Don't forget to save the handle of the plot (just after you created it) in a way you can retrieve it from your callback, but I assume you know how to do that since in your original code your callback was already calling the variable vectorx and vectory.

Obviously, do the same thing for the second plot and checkbox.

OTHER TIPS

This is a demo on how one can achieve what you are trying to do. Replace the data used in these codes with yours. The codes assume that axes tag is axes1 -

%%// --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)

vectorx = 1:50;
vectory1 = sin(vectorx);
if get(hObject,'Value')
    plot(vectorx,vectory1);
else
    cla(handles.axes1);
end

return;


%%// --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)

vectorx = 1:50;
vectory2 = cos(x2);
if get(hObject,'Value')
    plot(vectorx,vectory2);
else
    cla(handles.axes1);
end

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