Adjusting slider to scroll trough each image in 3D stack in GUI

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!

 Akzeptierte Antwort

Stephen23
Stephen23 am 10 Aug. 2017
Bearbeitet: Stephen23 am 21 Dez. 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 Kommentare

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: see edited answer.
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});
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.
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});
Try
set(handles.image, 'CData', handles.imagedata{round(index)});
That was the problem!
Now the slider works!
Any idea on how to display current slice number in the textbox below in GUI?
@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.
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?
@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?
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?
I never use GUIDE (writing my own code is much simpler), but probably something like this will work:
set(textHandle, 'string',get(hObject,'String'))
when put inside the slider callback, and given some textbox handle...
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.
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
Mario am 10 Aug. 2017
Bearbeitet: Mario am 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
Stephen23 am 10 Aug. 2017
Bearbeitet: Stephen23 am 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".
@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.
Stephen23
Stephen23 am 11 Aug. 2017
Bearbeitet: Stephen23 am 11 Aug. 2017
"Why is it so hard to write this code (as I saw from many other discussions) using GUIDE?"
No idea. I have never used GUIDE and have always written my own GUIs from scratch.
That "four line solution" does not require GUIDE: just put it in a script and run it. It works.
Mario
Mario am 11 Aug. 2017
Bearbeitet: Mario am 11 Aug. 2017
I finally found a solution to this problem.
this link helped me a lot!
So for anyone strugling with this, I advise to check the above link.
Thank you all for your help!
EDIT Yes, true. But I managed, by using this link above, write a code in GUIDE and make it work.
Hi Mario,
Can you upload the final code you've written?
Cheers

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Arjun Chawla
Arjun Chawla am 20 Dez. 2017

0 Stimmen

Hi Mario, can you please upload your final gui code, that would be greatly appreciated. Thanks!

Kategorien

Mehr zu Graphics Performance finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 10 Aug. 2017

Bearbeitet:

am 21 Dez. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by