Sliders - How to change value in a TextBox

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

Stephen23
Stephen23 on 29 Aug 2016
Edited: Stephen23 on 29 Aug 2016
Here is a minimal working example:
function temp1
fgh = figure();
txh = uicontrol(fgh,'Style','text');
sld = uicontrol(fgh,'Style','slider','position',[90,90,200,20],...
'Callback',@updateText);
%
function updateText(h,~)
val = get(h,'Value');
set(txh,'String',num2str(val))
end
end
I also fixed your code by:
  1. removing the awful and totally unnecessary global. Because you are using nested functions (which are much, much better than globals) there is absolute no point in using globals. In any case you had not used globals properly, because in MATLAB globals must be declared in every function that uses them, whereas you only declared them one in the top level function. Tip for the future: don't use globals, you will only regret it.
  2. formatting it correctly: simply select all and click ctrl+i. Much tidier!
  3. using simpler callback calls that use variables form the main function, rather than passing them as arguments.
  4. Made the text box large enough so that the changing part of the text is actually visible.
I attached the working code here (yes, the text updates with the slider):

9 Comments

Yes that works, thanks! But why didnt it work before?
Stephen23
Stephen23 on 29 Aug 2016
Edited: Stephen23 on 29 Aug 2016
PS: some handy advice on globals:
"I have never seen MATLAB code where globals were the right thing to do"
The MATLAB docs recommend that passing arguments and using nested functions as best practice:
"Avoid global variables — Minimizing the use of global variables is a good programming practice, and global variables can decrease performance of your MATLAB code."
I do know passing variables, recursive programming etc. But that has not changed, the only thing you ahve used is num2str, before i had a simple string already there? That doesnt work! Why? I dont see any reason why a normal string doesnt work, but a num2str does?
Stephen23
Stephen23 on 29 Aug 2016
Edited: Stephen23 on 29 Aug 2016
@Markus Stiller: actually your code was updating the text, and there was nothing wrong with the string. You just made the text box too small to see the non-static part. See point four of my list of improvements.
Lol, thank you. M;ay i ask you another question: in plot_m(val,handle), i use handle to change the parameters...is this the intended use of these handles i always read about? Or do i miss sth here? Thanks
Here are all three mentions of that function in your code:
plot_m(val,1)
plot_m(get(hObject,'Value'),2)
function plot_m(val,handle)
The last one defines the function, the first two call the function. There is no handle involved anywhere in this (badly named) variable. It is not a handle at all, it is just an integer that you define in your code. I have no idea why you call it a handle, but you should really change this very misleading name because in MATLAB handles are specific data types that are used to provide a handle to some graphics object, some other object, or function. For example in my (improved) code the variables fgh and txh are the only two handles in the whole code, being handles to the figure object and text object respectively.
So, having clarified that handle is not a handle, there is certainly nothing wrong with using an integer to select between options, as you are doing here.
PS: please accept my answer if it resolves your original question. We are all volunteers, and accepting answers is an easy way for you to politely thank us for our time helping you.
Sry, i did not know that this accepting is of importance. I will do that. Regarding what you have just written, this is exactly my question. For example see:
Using these handles, just gives errors. So i have made that workaround with the if-statement...How can i do this correctly using handles? Thx
Stephen23
Stephen23 on 29 Aug 2016
Edited: Stephen23 on 29 Aug 2016
Learning MATLAB from the documentation would be better than trying to figure it out from incomplete information like in that question. That question relates to using a structure that is saved and returned by guidata. The handles variable is simply a structure containing many handles to graphics objects. It can be used to pass data between callbacks.
You could use guidata if you really wanted to, but there is not really any point as you are already using nested functions. It is not clear to me why you want to use guidata, or what problem you imagine that using guidata would solve for you.
Ok thanks for you help :)

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!