Adjusting slider to scroll trough each image in 3D stack in GUI
10 次查看(过去 30 天)
显示 更早的评论
Mario
2017-8-10
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!
采纳的回答
Stephen23
2017-8-10
编辑:Stephen23
2017-12-21
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 个评论
Mario
2017-8-10
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
2017-8-10
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});
Mario
2017-8-10
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
2017-8-10
That was the problem!
Now the slider works!
Any idea on how to display current slice number in the textbox below in GUI?
Stephen23
2017-8-10
@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
2017-8-10
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
2017-8-10
编辑:Stephen23
2017-8-10
@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
2017-8-10
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
2017-8-10
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
2017-8-10
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
2017-8-10
编辑:Mario
2017-8-10
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
2017-8-10
编辑:Stephen23
2017-8-10
@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
2017-8-11
@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.
更多回答(1 个)
Arjun Chawla
2017-12-20
Hi Mario, can you please upload your final gui code, that would be greatly appreciated. Thanks!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)