how can i give the variable of a slider to another function?

6 views (last 30 days)
Hello,
sorry for my bad english..
I have created four Sliders in gui, and show their value in an edit text box.
Now i want to use the four variables of the slider (var1, var2, var3 und var4) in an other function to plot a graph.
In the function "function out = chua_function(t,p)" i want to write "a = var1, b = var2,.." instead of "a =16, b = 1,.."
how can i do this? i have tried this with global variables, but it don't worked.
Heres my code:
function varargout = chaos_simulation_gui(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @chaos_simulation_gui_OpeningFcn, ...
'gui_OutputFcn', @chaos_simulation_gui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
function chaos_simulation_gui_OpeningFcn(hObject, eventdata, handles, varargin)
[t,y] = ode45(@chua_function,[0 150],[1 0 0]);
axes(handles.axes1);
plot3(y(:,1),y(:,2),y(:,3)) %3D Plot
xlabel('D^-^1 Region')
ylabel('D^0 Region')
zlabel('D^1 Region')
grid
...
% Choose default command line output for chaos_simulation_gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = chaos_simulation_gui_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
function slider_a_Callback(hObject, eventdata, handles)
val1=get(hObject,'value');
set(handles.edit1,'string',num2str(val1));
guidata(hObject,handles);
function slider_a_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
...
function slider_n_Callback(hObject, eventdata, handles)
val4=get(hObject,'value');
set(handles.edit4,'string',num2str(val4));
guidata(hObject,handles);
function slider_n_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function edit1_Callback(hObject, eventdata, handles)
value1=get(hObject,'string');
set(handles.text,'value',num2str(value1));
guidata(hObject,handles);
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
...
function edit4_Callback(hObject, eventdata, handles)
value4=get(hObject,'string');
set(handles.text,'value',num2str(value4));
guidata(hObject,handles);
function edit4_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function out = chua_function(t,p)
a = 16; % here i want to write a = val1
B = 30; % here i want to write b = val2
m = -1.2;
n = -0.7;
x = p(1);
y = p(2);
z = p(3);
g = n*x+0.5*(m-n)*(abs(x+1)-abs(x-1)); %St�ckweise Lineare Gleichung der Diode
xdot = a*(y-x-g);
ydot = x - y+ z;
zdot = -B*y;
out = [xdot ydot zdot]';

Answers (1)

Bhaskar R
Bhaskar R on 21 Nov 2019
In the end of evert UI control funtion make a statement as
guidata(hObject,handles); % which enables the to pass/updates varibles
Then modify your function as
Your required data variables var1, var2, var3 and var4 already globally visiable with the GUI handles as in the edit boxes(you have set in each UI control of slider) so you need to modify in the called/defination function as
function out = chua_function(hObject,handles)
% you need to get variables from handles
val1 = str2num(handles.edit1.String);
val2 = str2num(handles.edit2.String);
val3 = str2num(handles.edit3.String);
val4 = str2num(handles.edit4.String);
% a = 16; % here i want to write a = val1
% then
a = val1;
% B = 30; % here i want to write b = val2
B = val2;
m = -1.2;
n = -0.7;
x = p(1);
y = p(2);
z = p(3);
g = n*x+0.5*(m-n)*(abs(x+1)-abs(x-1)); %St�ckweise Lineare Gleichung der Diode
xdot = a*(y-x-g);
ydot = x - y+ z;
zdot = -B*y;
out = [xdot ydot zdot]';
guidata(hObject,handles);
  4 Comments
Rik
Rik on 21 Nov 2019
Comment posted as answer by Albin Trog:
Sorry but I am completely new in Matlab.. How must I modify anything in the handles struct? Yes, I want to affect the function with the values from the sliders. Where must I move the ode45?
Rik
Rik on 21 Nov 2019
Please don't post your comments in the answer section.
You don't need to modify the handles struct at all.
If you want to use the values of the sliders in a function, the best way is probably to have a button. Then you can set the sliders to your chosen values and put the call to ode45 and your plotting functions in the callback function of the button. You should be aware that the OpeningFcn runs only once: when the GUI starts.
If you are confused by GUIDE: don't feel bad, it is a mess. If you are confused by Matlab, try OnRamp. For a more thorough discussion about your options when it comes to GUIs: how to create a GUI.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks 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!