Multi-parameter function plot with GUIDE

5 views (last 30 days)
Julien
Julien on 9 Apr 2012
Hi all,
I'm trying to use GUIDE to plot a function that depends on 5 different parameters.
I'd like my plot to vary according to the parameters, which I want to change through sliders. What I've done so far is for each slider callback function, I'm redefining all the parameters according to the value of each sliders, and then plotting the function. (so I basically have 5 times the same code, with only one line changing each time: the parameter corresponding to the slider takes a new value). If I don't redefine all my variables, Matlab says that the other parameters are not defined.
Everything seems to work, but I feel like this is a poor code. Is there a better way to do this? (say only plot the function once, and for each slider only vary one parameter?)
Thanks in advance.
Julien

Answers (1)

Sean de Wolski
Sean de Wolski on 9 Apr 2012
Everything seems to work, but I feel like this is a poor code. Is there a better way to do this? (say only plot the function once, and for each slider only vary one parameter?)
How are you plotting the data?
I would recommend taking the "plot once modify later" approach. To do this, save the handle ( setappdata and the handles structure (or get the children of the axes)) to the plotted object (line, image, surface, - whatever it is) and maybe the axes, and update it's properties based on the new values. This could mean getting the 'xdata', or 'ydata', or 'linewidth, updating it and setting it. Since this probably isn't clear at all, here's pseudoish code for a slider callback:
function slidercallback(hObject,eventdata,handles);
hLine = get(handles.axes1,'children'); %get handle to children of axes1
%let's assume it's one line for this example
set(hLine,'linewidth',get(hObject,'value')); %set line width to slider value
  1 Comment
Julien
Julien on 9 Apr 2012
Hi,
Thanks for your answer.
I still don't understand how setappdata is supposed to work however, despite the fact that I've looked at it in the help section... I'll look more into details about it.
In the mean time, I'll give an example of the approach I had chosen:
Say I want to plot the function y=ax+b.
I have two sliders: "sliedra" and "sliderb" which are suppose to allow me to manually pick the value of a and b respectively.
My code looks something like this
function slidera_Callback(hObject,eventdata,handles);
a = get(handles.slidera,'Value');
b = get(handles.sliderb,'Value');
x = 0:0.01:2;
y = ax+b;
plot(x,y);
function sliderb_Callback(hObject,eventdata,handles);
a = get(handles.slidera,'Value');
b = get(handles.sliderb,'Value');
x = 0:0.01:2;
y = ax+b;
plot(x,y);
I guess the right approach would be to have something like :
function slidera_Callback(hObject,eventdata,handles);
a = get(handles.slidera,'Value');
function sliderb_Callback(hObject,eventdata,handles);
b = get(handles.sliderb,'Value');
And a way to automatically update the axes1, whenever a value changes...
But even though that's what I did at first, whenever I moved one of the sliders (say a) my code would tell me that b wasn't defined... which is why I had to redefine, for each slider, all the parameters and ask the code to plot my function again...

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!