Sliders - How to change value in a TextBox
Show older comments
Hi, i am currently learning how to use Matlab for a Program i am going to write. At the moment, I want to use a Slider to change a Parameter/Change the Value in a TextBox when I change the Slider (it does not need to be continuously - when the mouse is released, that is enough as trigger). I have found several examples because this is obviously "easy" - but none of them works. I always get errors like "not enough input parameters" or too many of them. Since nobody bothers here to give a full example but rather writes only 1 line which is supposed to explain everything, i am stucked at this problem. Below you will find the program i wrote. I want to display the value of Slider1 in the gammatext box. It doesn't change however, only the display command works - the set command has no effect. Some explanation would be very nice (also regarding the rest of the program :), eg. with this "handles" - that never worked either - the buildaround you can see below ) Thank you very much, Markus
function MagSupConII
%Build GUI
global ampl k mu0 s1;
init_ui;
init;
function init_ui()
f1=figure('position',[200 100 800 800], 'name','MagSupConII','NumberTitle','off');
gammatext = uicontrol('Style', 'text','Position', [20 13 50 15]);
slider1 = uicontrol('Style','slider',...
'Min',0,'Max',1,'Value',0.5,'SliderStep',[0.5 0.5],...
'Position',[100 10 200 20],...
'CreateFcn', {@slider1_Callback,gammatext},...
'Callback', {@slider1_Callback,gammatext});
slider2 = uicontrol('Style','slider',...
'Min',0,'Max',10,'Value',3,...
'Position',[100 40 400 20],...
'CreateFcn', @slider2_Callback,...
'Callback', @slider2_Callback);
end
function init()
mu0=1.2566370614E-6;
ampl = 1;
k = 1;
end
function slider1_Callback(hObject,~,gammatext)
val=get(hObject,'Value');
s1=sprintf('Gamma = %2.1f',val);
display(s1)
set(gammatext,'String',s1);
plot_m(val,1)
end
function slider2_Callback(hObject,~,handles)
plot_m(get(hObject,'Value'),2)
end
function plot_m(val,handle)
if handle == 1
k = val;
elseif handle == 2
ampl = val;
end
x=0:0.01:10;
y=ampl*sin(k*x);
plot(x,y);
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Entering Commands 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!