Question

I have GUI in Matlab that I have made using the Programmatic approach. It has 6 sliders and I want to be able to move one of them and have the others update as if i clicked them again but to stay in the same place. I am guessing I will need to use the set() function. Is there some function to do this in matlab already? I have been looking around. Any suggestions or something to point me in the right direction?

Was it helpful?

Solution

If you are using guide, you can access the other sliders from the handles variable that is available in each callback.

Set the Value property for them.

   function Slider1_CallBack(hObj,evt,handles)
       set(handles.Slider1,'Value',10);
       set(handles.Slider2,'Value',10);
      % etc..
   end

In case you are using it programmaticaly, you can store the handles manually.

   function main
        handles.Figure1 = figure(..);
        handles.Slider1 = uicontrol(...);
        handles.Slider2 = uicontrol(...);
        guidata(handles.Figure1,handles);
   end

And your slider callback should be:

   function Slider1_CallBack(hObj,evt)
       handles = guidata(hObj);
       set(handles.Slider1,'Value',10);
       set(handles.Slider2,'Value',10);
      % etc..
   end

Edit A good practice in writing UI is separating the GUI logic from the actual data. You always change the data, and call updateGUI routine.

Therefore, you can write your program like this:

   function main
        handles.gui.Figure1 = figure(..);
        handles.gui.Slider1 = uicontrol(...);
        handles.gui.Slider2 = uicontrol(...);

        handles.data.x = 1;
        guidata(handles.Figure1,handles);
   end

   function UpdateGui(handles)
        %Based on the data, update the GUI
        set(handles.Slider1,'Value',handles.data.x);
        set(handles.Slider2,'Value',handles.data.x+1);
   end 

And the callback should look like:

   function Slider1_CallBack(hObj,evt)
       handles = guidata(hObj);
       handles.data.x = handles.data.x  + 1;
       UpdateGui(handles);
       guidata(hObj,handles);
      % etc..
   end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top