You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Adjusting slider to scroll trough each image in 3D stack in GUI
4 views (last 30 days)
Show older comments
Hi,
I am writing a small GUI (it is in the attachment) and I am having trouble adjusting the slider to work so that when I use the slider I will be able to scroll trough each image in a 3D stack.
Currently I am getting an error:
Warning: slider control can not have a Value outside of Min/Max range
and I am not sure how to solve it.
I am also trying to display the number of the current slice in this small GUI.
Any help would be appreciated.
Thanks!
Accepted Answer
Stephen23
on 10 Aug 2017
Edited: Stephen23
on 21 Dec 2017
Change the order in which you specify the parameters:
set(handles.hslider, 'Value', 1, 'Min', 1, 'Max', numel(filenames), ...
'SliderStep', [1,1]./(numel(filenames) - 1))
Otherwise you have a conflict because you change the Min value and this puts the default Value outside the min-max range (the input arguments are processed from left to right).
Addendum: to update a value continuously as a slider is moved see this answer:
20 Comments
Mario
on 10 Aug 2017
Hi Stephen,
When I change the order, I get an error:
Error using set
Value must be a 2 element vector
Error in untitled1>pushbutton1_Callback (line 127)
set(handles.hslider, 'Value', 1, 'Min', 1, 'Max', numel(filenames), ...
Mario
on 10 Aug 2017
Yea, now that line of code works! Great!
But, now I got another error in hslider function:
Subscript indices must either be real positive integers or logicals.
Error in untitled1>hslider_Callback (line 83)
set(handles.image, 'CData', handles.imagedata{index});
Stephen23
on 10 Aug 2017
Edited: Stephen23
on 10 Aug 2017
if numel(handles.imagedata)>=index
set(handles.image, 'CData', handles.imagedata{index});
end
Most likely this happens when the objects are being initialized. You could check that by adding an else and displaying some the value in the command window.
Mario
on 10 Aug 2017
I tried something similar before you posted your answer, but no luck.
I also tried your solution and I get an error:
Subscript indices must either be real positive integers or logicals.
Error in untitled1>hslider_Callback (line 82)
set(handles.image, 'CData', handles.imagedata{index});
Mario
on 10 Aug 2017
That was the problem!
Now the slider works!
Any idea on how to display current slice number in the textbox below in GUI?
Stephen23
on 10 Aug 2017
@Mario: add a textbox. Set its string using set.
PS: You should accept the answer that best resolves your original question. This is an easy way for you to thank the volunteers who helped you.
Mario
on 10 Aug 2017
Hi Stephen,
Your help was greatly appreciated. Thanks!
Regarding the second part of the problem, I tried to write code for displaying current slice number in the textbox below in GUI, but I was not successful.
I placed my code under the textbox that I named textBox:
function textBox_CreateFcn(hObject, eventdata, handles)
sliderstate = get(handles.hslider,'value'); % Get the slider's info
enteredvalue = str2double(get(handles.textBox,'string')); % The new frame number
if enteredvalue >= sliderstate{1} && enteredvalue <= sliderstate{2} %Check if the new frame number actually exists
slidervalue=round(enteredvalue);
set(handles.hslider,'value',slidervalue) %If it does, move the slider there
else
set(handles.hslider,'string',sliderstate{3}) %User tried to set slider out of range, keep value
return
end
Any idea on how to finish it properly?
Stephen23
on 10 Aug 2017
Edited: Stephen23
on 10 Aug 2017
@Mario: I do not understand what you want. Your code does the opposite of what you say you want to do. You state that you want to "display current slice number in the textbox below in GUI", but this line
enteredvalue = str2double(get(handles.textBox,'string'));
gets the text value and then you proceed to use it for setting the slider value. Do you want both the text and slider to update each others values? If the text is only for displaying the value (as you wrote) why do you get its value now?
Mario
on 10 Aug 2017
I tried that option to see if I can make it work, but was unsuccessful. Now I focus only on displaying the value inside textBox.
How can I only store current slice number inside textBox while moving hslider?
Mario
on 10 Aug 2017
I used your code, and replaced texbox handle with mine:
set(handles.textBox, 'string',str2double(get(hObject,'String')))
now in the GUI where the texbox should display a current slice number, it displays NaN while I move the slider trough the images.
Walter Roberson
on 10 Aug 2017
if hObject is your slider then
set(handles.textBox, 'string', get(hObject,'Value') )
Stephen: it would not make sense to convert string to double to set the double as a string field: if you already had a string field then just copy it directly.
Mario
on 10 Aug 2017
Edited: Mario
on 10 Aug 2017
Hi Walter,
the handle to my slider is defined as handles.hslider
When I type your code, the text box displays only 1 while I move the slider trough the image stack.
I tried even with:
set(handles.textBox, 'string', get(handles.hslider,'Value') )
but the result is the same.
Stephen23
on 10 Aug 2017
Edited: Stephen23
on 10 Aug 2017
@Mario: The behavior of the slider uicontrol object is to change the value after releasing with the mouse. The is explained clearly in the documentation: " The location of the thumb indicates a numeric value, assigned to the Value property when you release the mouse button."
If you want the value to be updated while you are sliding the bar then you will need to add a listener. Exactly how depends on the MATLAB version that you have. In any case you will find many examples on this forum, so use the search field in the top corner and search for "slider listener".
Mario
on 11 Aug 2017
@Stephen: thanks you for your valuable suggestions and links. I am trying to write a code using listeners based from those links that you provided me, but no luck.
Why is it so hard to write this code (as I saw from many other discussions) using GUIDE?
I tried your "4 line of code" solution to test it that you mentioned here and I am still confused on how to proceed with this using GUIDE.
I even tried @Jan Simon solution, but did not solve the problem.
More Answers (1)
Arjun Chawla
on 20 Dec 2017
Hi Mario, can you please upload your final gui code, that would be greatly appreciated. Thanks!
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)