The slider is not working. It won't move either under wheel scrolling or arrowheads pressing. If it slightly moves, it goes back to the beggining instantly and it won't control the image depicted on the first axes1, it won't alter.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Why slider is not working in my GUI?
25 views (last 30 days)
Show older comments
I have created a GUI consisted of 4 axes, a push button and a slider. Although, the slider commands (callbacks, uicontrols, guidata etc) including the mouseScroll function, are perfectly working, because they have been tested to a different figure (not my GUI) consisted of a single hFig and an axes1 and it perfectly work, but when I try to implement the same commands to my GUI it fails.
You will notice to the m files I attach, that I have placed the same commands over and over again to almost all the relative callback functions (slider1_Callback, mouseScroll, loadButton, etc) because they were asking for the variables to be stated.
Any help on this?
1 Comment
Accepted Answer
Jan
on 11 Jun 2018
You are creating a new slider inside the callback of the slider:
handles.SliderFrame = uicontrol('Style','slider','Position',[60 20 400 50], ...
'Min',1,'Max',NumFrames,'Value',1, ...
'SliderStep',[1/NumFrames 2/NumFrames], ...
'Callback',@slider1_Callback);
This hides the formerly existing sliders and sets the slider value to 1 for each call.
Importing the list of files in all slider callbacks is a waste of time in addition.
Solution: Create the slider once only, either in GUIDE or in the OutputFcn (where it is done now already also). Then omit the re-creation by uicontrol.
I suggest to call the slider1_callback from the mouseScroll function.
27 Comments
Stelios Fanourakis
on 11 Jun 2018
Shall I call the slider1_Callback from mouseScroll using the same uicontrol line?
Stelios Fanourakis
on 11 Jun 2018
if I won't add the uicontrol line (handles.SliderFrame) in the slider1_Callback Fcn, I get the error
"Reference to non-existent field 'SliderFrame'.
Error in fresh>slider1_Callback (line 147) CurrentFrame = round((get(handles.SliderFrame,'Value')));
Error while evaluating UIControl Callback."
Stelios Fanourakis
on 11 Jun 2018
If I edit the whole function and have them all as nested functions and not as separate individually functions, how eligible will I be to re edit the fig if I want to later on?
Jan
on 12 Jun 2018
Shall I call the slider1_Callback from mouseScroll using
the same uicontrol line?
What is a "uicontrol line"?
if I won't add the uicontrol line (handles.SliderFrame) in the
slider1_Callback Fcn, I get the error
"Reference to non-existent field 'SliderFrame'".
The message is clear: handles.SliderFrame has not been defined before. But it is a bad idea to create it inside the callback function. You create it in the Load_image_Callback also, but I assume this is a bad idea also. But I do not know your intention to do so. I assume you should create all uicontrols either in GUIDE or in the OpeningFcn. The dynamic creation inside other callbacks seems to be too confusing.
I assume the original code can be fixed very easily. Rewriting the code to use nested functions will most like introduce new problems. But I do not know, what "editing the fig" exactly means.
Stelios Fanourakis
on 12 Jun 2018
What is a "uicontrol line"?
This one: handles.SliderFrame = uicontrol('Style','slider','Position',[60 20 400 50], ... 'Min',1,'Max',NumFrames,'Value',1, ... 'SliderStep',[1/NumFrames 2/NumFrames], ... 'Callback',@slider1_Callback);
I assume you should create all uicontrols either in GUIDE or in the OpeningFcn. The dynamic creation inside other callbacks seems to be too confusing.
Where in GUIDE exactly you mean? I have tried all possible positions to create the slider but it doesn't seem to fix the problem.
But I do not know, what "editing the fig" exactly means.
In case I want to modify the fig, somehow. Will I be able to do it if I rewrite the code to use nested functions?
Jan
on 12 Jun 2018
You can insert a slider object in GUIDE. I do not know what "where in GUIDE" means. There is the group of buttons on the left side, where you can select the uicontrol. Simply select the slider object and insert it.
Using code works also. But the point is: do this once only. If you create a new slider inside the callback of the slider, you get a pile of sliders, which conceal each other.
If you use GUIDE, the callbacks are created automatically in the corresponding M-file. If you modify this M-file by a complete redesign moving all function to nested functions, the chaos is near.
Considering that all you have to move the line, which creates the slider from the callback to the OpeningFcn, this discussion is far too complicated.
Stelios Fanourakis
on 12 Jun 2018
If it was to insert the slider as a code in GUI [ not from the panel of GUIDE], at what point (function) should I insert it? I tried to code the slider myself and all I concluded is that all Functions (slider1_Callback, OpenFcn, etc) needs to define the slider and the image again and again. They cannot take it as input from other functions. This is my problem. It get very complicated because I need to define in every separate function the slider and its parts (see uicontrol line) again and again. Let me try your idea and I'll come back again
Stelios Fanourakis
on 12 Jun 2018
I actually need to use that uicontrol line that creates the slider and integrate it to my GUI made of GUIDE. If I move this uicontrol line to the OpeningFcn then the slider1_Callback won't recognize their components like NumFrames, MyMatrix etc. That's why Matlab prompts me to define again and again the components in every Callback function.
Do you know how to do it run using this uicontrol line and its components?? I don't want to use the ready slider from GUIDE
Jan
on 13 Jun 2018
If it was to insert the slider as a code in GUI [ not from the
panel of GUIDE], at what point (function) should I insert it?
Did you read my messages? In the OpeningFcn.
I concluded is that all Functions (slider1_Callback, OpenFcn,
etc) needs to define the slider and the image again and again
This conclusion is not correct.
They cannot take it as input from other functions.
A callback is triggered, when a GUI element is activated. There is no calling function, at least not in the level of the GUI. If you want to share data between callbacks, store them in the handles struct or in the ApplicationData or UserData of a GUI element. You have done this already by appending a field to this struct. With the command guidata(hObject, handles) the updated struct is stored in the figure again, such that the new values are available in the next call of any other callback function.
If I move this uicontrol line to the OpeningFcn then the
slider1_Callback won't recognize their components like
NumFrames, MyMatrix etc.
Then this is another problem. Maybe you forgot to update the modified handles struct with guidata(). But in every case, recreating the slider is the wrong option.
It makes absolutely no difference if you create the slider in GUIDE or by code. Therefore I cannot understand, why you do not want to do this in GUIDE and assume this is based on a misunderstanding.
I suggest to examine these excellent examples: FEX: 41 GUI examples . I hope they clarify how data are shared between callbacks.
I cannot fix the code you have posted directly for you, because I do not understand its purpose completely. But I suggest the following procedure: You try to consider the advice given in the forum by modifying your code. Then post the new code and the new error message, or an explanation of the difference between the results and your expectations. After some iterations, your code will work, and it will be much simpler than the initial version.
Stelios Fanourakis
on 13 Jun 2018
I had guidata(hObject,handles) at the beggining and at the end of each function. Still at each function I got the error "Reference to non - existent field .." mentioning a part of the uicontrol like the image, or other. That's why I was adding them again and again.
Can you please send me a sample of code that runs all images in a sequence of images (stack of images) to the axes1? Let's assume that I got MyMatrix which is a set of 15 dicom images. I want to drag the slider from 1 to 15 and alter the image at each dragging
Stelios Fanourakis
on 13 Jun 2018
Now, I get the error "Index exceeds matrix dimensions" at " imshow(squeeze(MyMatrix(:,:,CurrentFrame, :)), 'parent', handles.axes1);" line.
As I told you before, this code has been tested to another simple fig, irrelevant to GUIDE and it perfectly works
Stelios Fanourakis
on 13 Jun 2018
Can you explain me possible fault or solution for the above error?
Jan
on 13 Jun 2018
I had guidata(hObject,handles) at the beggining and at the end
of each function
Do not call this at the beginning of each function.
Without seeing the current version of the code, there is no chance to guess, what it still going wrong.
Stelios Fanourakis
on 13 Jun 2018
Here is the new version of the code. Please note that I only have assigned the slider using uicontrol at the OpeningFcn. I get the error
"Index exceeds matrix dimensions" at " imshow(squeeze(MyMatrix(:,:,CurrentFrame, :)), 'parent', handles.axes1);" line.
Stelios Fanourakis
on 13 Jun 2018
It cannot be wrong. The same code works to another .m file without the GUI made by GUIDE.
Jan
on 14 Jun 2018
It cannot be wrong.
If you get an error, it is wrong. Promissed.
In OpeningFcn
handles = guidata(gcf);
This is dangerous. Do not rely on the current figure, when you have a handle, which is guaranteed to be valid.
handles = guidata(hObject);
But handles is provided in the inputs already. So simply omit this line.
frameindex = max(1, min(handles.frameindex, NumFrames));
frameindex is not used here. So omit this line also.
In pushbutton1_Callback omit the line axes(handles.axes1). You do not have to make axes1 the current axes, because this wastes time only. You have specified this axes as Parent in imshow and this is much better.
Here you set the ApplicationData of the pushbutton1:
setappdata(hObject,'MyMatrix',MyMatrix);
But later you request 'MyMatrix' from the slider object. Better:
setappdata(ancestor(hObject, 'figure'), 'MyMatrix', MyMatrix);
In slider1_Callback
See above: Omit handles = guidata(gcf), because handles is provided in the inputs already.
See above:
% MyMatrix = getappdata(hObject,'MyMatrix'); Replace by
MyMatrix = getappdata(ancestor(hObject, 'figure'), 'MyMatrix');
hObject is the handle of the GUI element, which triggered the event. Inside the slider callback it is the slider. So use the figure instead for storing the ApplicationData.
Maybe obtaining the ApplicationData from the wrong object was the reason of the problems. Actually this should cause an error, but perhaps you have stored some ApplicationData and saved the figure afterwards?
Stelios Fanourakis
on 14 Jun 2018
Somehow better outcome but not exactly what I want.
The slider1_Callback Fcn still has a lot of flaws. Yet, it cannot recognise terms like NumFrames although it is defined at the OpeningFcn, or SliderFrame and again it is defined in the OpeningFcn.
Moreover, it gives an error such as "Not enough input arguments" to the CurrentFrame = round((get(handles.SliderFrame,'Value'))); line.
Still, I am confused since those lines work to another m file but I need to drastically modify them here.
Jan
on 15 Jun 2018
@Stelios: I could give a further advice, if you post the updated code. "It cannot recognize terms" and "Not enough input arguments" is not useful for a discussion in the forum. Prefer to post the code and the complete error message.
Stelios Fanourakis
on 15 Jun 2018
Here are the updated codes and here are the errors:
Error using images.internal.imageDisplayValidateParams>validateCData (line 115)
Multi-plane image inputs must be RGB images of size MxNx3.
Error in images.internal.imageDisplayValidateParams (line 27)
common_args.CData = validateCData(common_args.CData,image_type);
Error in images.internal.imageDisplayParseInputs (line 78)
common_args = images.internal.imageDisplayValidateParams(common_args);
Error in imshow (line 241)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
Error in peirama>slider1_Callback (line 142)
imshow(squeeze(MyMatrix(:,:,CurrentFrame,:)), 'parent', handles.axes1);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in peirama (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)peirama('slider1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Not enough input arguments.
Error in peirama>slider1_Callback (line 140)
set(handles.Edit1,'String',num2str(CurrentFrame));
Error while evaluating UIControl Callback.
Jan
on 16 Jun 2018
Edited: Jan
on 16 Jun 2018
The first error message is clear:
Multi-plane image inputs must be RGB images of size MxNx3
This means, that the 3rd dimension of squeeze(MyMatrix(:,:,CurrentFrame,:)) does not have the length 3. What is the 4th dimension of MyMatrix? I guess you mean:
MyMatrix(:,:,:, CurrentFrame)
By the way: Then you can omit the squeeze .
Stelios, please start to use the debugger. You can set breakpoints in the failing code and examine the values of the used variables.
CurrentFrame = round((get(ancestor(hObject, 'SliderFrame'), 'Value')));
set(handles.Edit1,'String',num2str(CurrentFrame));
ancestor(hObject, 'SliderFrame') cannot work, because there are no objects of type 'SliderFrame' in Matlab. But you have defined handles.SliderFrame in the OpeningFcn. So an educated guess is:
CurrentFrame = round(get(handles.SliderFrame, 'Value'));
set(handles.Edit1, 'String', num2str(CurrentFrame));
You limit the values of the "CurrentFrame" afterwards:
max(1, min(CurrentFrame, NumFrames))
Wouldn't it be better to restrict the values before you display them in the Edit1 field?
You set the title of the axes twice:
title(handles.axes1, sprintf('frame #%d', CurrentFrame));
title(handles.axes1, basename);
Stelios Fanourakis
on 16 Jun 2018
Edited: Stelios Fanourakis
on 16 Jun 2018
Dear Jan. Again I attach the changes you suggested me to do.
Stelios Fanourakis
on 16 Jun 2018
What I noticed is: I drag the down slider once (either left or right). I get the error "Not enough input arguments" for line "CurrentFrame = round(get(handles.SliderFrame, 'Value'));"
Then, I click on the upper slider, either left or right and it works for one step (the image is changing), to the direction similar to the down slider I clicked previosuly. So, if I click the down slider at right once, I get an error, then I click on the upper slider once and the image is changing by once on the right.
This is very strange. Can you guess what is the solution?
Jan
on 17 Jun 2018
Are you really sure, that this line causes an error:
CurrentFrame = round(get(handles.SliderFrame, 'Value'));
Then you have redefined round() or get(). Check this:
which round -all
which get -all
Do you see a user-defined function which shadows the built-in version?
I do not understand, what "drag down (either left or right)" and the "upper slider" means. There is one slider only.
My problem remains, that I do not even understand, what the code should do. You read an image stack by dicomread ar first. Afterwards you read a list of files names in each callback of the slider. Then you display a slice from the dicom image, adjust the CurrentFrame value to the number of available files, read another dicom image, overwrite the formerly displayed image and replace the title. At least half of the actions are a waste of time only, because the results are removed before the user can see it.
Clean up the code until it does only, what you want it to do. Insert meaningful comments, such that the purpose gets clear. Use the debugger to step through the code line by line to understand, what happens.
And whenever you mention an error in this forum, post a copy of the complete message.
Stelios Fanourakis
on 17 Jun 2018
The whole error message is
"Not enough input arguments.
Error in peirama>slider1_Callback (line 135)
CurrentFrame = round(get(handles.SliderFrame, 'Value'));
Error while evaluating UIControl Callback.
Although, peirama.fig is the same one I uploaded here, to my Matlab it appears two sliders. One up and one down. As I explained earlier, I first drag the down slider once, I get the above error message, and straight afterwards I drag the upper slider and it works for one step (one image is changing) towards the same direction as the down slider with the error did.
A bit more explanation of my code:
1. peirama_OpeningFcn: I define the folder where dicom images are. I set the uicontrol for the slider and the text that defines the current frame.
2. pushbutton1_Callback Fcn
By pressing the button, I choose the first image out of a series. Then, DicomReader (a user-defined fcn) creates the stack of images and outputs it as a 3D or 4D. Then, makeImIsoRGB fcn, takes the 3D/4D image and interpolates it for the voxel size to be isotropic.
MyMatrix is the final image. The rest makes sense, I suppose.
Jan
on 17 Jun 2018
@Stelios: Are you aware that I spend some time with trying to help you? You repeatedly ignore some of my questions for clarifications. Solving the actual problem would be most likely solved in a minute, if you post the required details.
I cannot open your fig file, because I do not have a Matlab version currently. I cannot imagine what "one up and one down" means. You have created one slider in the OpeningFcn and I see one callback for a slider also. You use the 'Value' property of one uicontrol only. So what it the job of the 2nd slider?
As far as I can see, you define the folder to read from in the OpeningFcn and in the Slider1_callback again.
This is the 25th comment. I strongly recommend to start to use the debugger. This is ways easier than posting some partial descriptions and waiting for the forum to guess the reasons of the problems. I will leave this discussion now, because it is obvious, that I cannot help you efficiently.
Good luck!
Stelios Fanourakis
on 17 Jun 2018
@Jan. You do helping me a lot with your suggestions. It seems I need to keep on forward myself from now and on. One slider I added on the GUIDE and one slider from uicontrol. When the figure is loading, it shows 2 sliders but only one of them actually works.
Now the bottom slider works but it works to infinity. I want the total range of the slider to be the number of image frames, e.g. 15
Stelios Fanourakis
on 17 Jun 2018
Debugger will only tell me the error point. It won't solve it.
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps 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!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 (한국어)