Why slider not working?
Show older comments
I got this function
function slider1_Callback(hObject,eventdata, handles,im2,S,sno)
% test it out - get the handles object and display the current value
handles.sliderImages = handles.im2;
im2 = handles.sliderImages
SIZE_Z = handles.SIZE_Z;
set(handles.slider1,'Value',1);
set(hObject,'Min',1,'Max',SIZE_Z);
set(hObject,'SliderStep',[1/(SIZE_Z-1),1/(SIZE_Z-1)]);
slider2Val=floor(get(hObject,'Value'));
currentSlice=slider2Val;
imshow(im2(:,:,currentSlice),[752 1512],'Parent',handles.axes1);
handles.currentSlice=currentSlice;
guidata(hObject,handles);
edit12_Callback(hObject, eventdata, handles);
And I get the error "Struct contents reference from a non-struct array object." at line
handles.sliderImages = handles.im2;
What can this mean?
9 Comments
Walter Roberson
on 1 Jun 2018
Edited: Walter Roberson
on 1 Jun 2018
You would have had to code your own Callback property in order to to be able to expect those im2, S, sno parameters. What did you set the Callback property to, and at what point in the program do you set it?
Stelios Fanourakis
on 2 Jun 2018
Stelios Fanourakis
on 3 Jun 2018
Walter Roberson
on 3 Jun 2018
When the problem occurs, what value does frameindex end up having, and what size is filelist ?
Stelios Fanourakis
on 3 Jun 2018
Walter Roberson
on 3 Jun 2018
Are you certain that when you are at the right end and start to slide backwards, that frameindex = 1? Unless you configure a slide direction to be reversed, the normal expected value at the right end would be the Max property, which would you set at NumFrames .
Please put in a
dbstop if error
and run the code, and when it fails, tell us the value of frameindex then, and also tell us what NumFrames is then (so we have something to compare against.)
I have a hypothesis but I need the numbers from you to be more certain.
Stelios Fanourakis
on 3 Jun 2018
Walter Roberson
on 3 Jun 2018
Type that at the command line and then run the code.
Stelios Fanourakis
on 3 Jun 2018
Answers (1)
Walter Roberson
on 2 Jun 2018
0 votes
You cannot bring a variable defined in another function into the current function just by mentioning its name in the function() definition.
If you are defining those variables in the load image callback, then you need to store those values somewhere that the slider callback can get at, such as in the handles structure (though storing an entire image in a handles structure is not the best idea for performance reasons.)
17 Comments
Walter Roberson
on 3 Jun 2018
frameindex = max(1, min(handles.frameindex, NumFrames) );
also I recommend testing NumFrames > 0 just in case.
Stelios Fanourakis
on 3 Jun 2018
Walter Roberson
on 3 Jun 2018
You might be able to track that more exactly if you give the command line command
dbstop if caught error
and then run.
Note: due to some questionable implementation logic, it is normal to get a caught error when initializing GUIDE generated code. You would command
dbcont
to continue on to the problem in your XListenerCallBack
Stelios Fanourakis
on 3 Jun 2018
Walter Roberson
on 3 Jun 2018
I would need the .fig and entire .m code and a representative data directory in order to test on your behalf. Otherwise I have to rely on you to type in commands and tell me the results.
Stelios Fanourakis
on 3 Jun 2018
Stelios Fanourakis
on 3 Jun 2018
Walter Roberson
on 3 Jun 2018
I do not understand the purpose of having both a slider callback and a listener ??
I do not understand the purpose of imshow() of data extracted from MyMatrix, and then going and imshow() of data read with dicomread() in the same routine, probably drawing on top of the MyMatrix output?
Stelios Fanourakis
on 4 Jun 2018
Stelios Fanourakis
on 4 Jun 2018
Walter Roberson
on 4 Jun 2018
Unfortunately, you did not describe your intention for having the two different imshow, so I cannot give you finished code. I have attached modified code that works as best practical considering my lack of knowledge about your intentions.
You will notice that the right hand image only changes once. There are two reasons combined for that:
- The slider Callback is being invoked only once, so you are setting frameindex only once. After that, the Listener callback is being invoked
- Your first imshow displays from CurrentFrame, but your second imshow reads according to frameindex and displays that. Because two different sources of data are being used, I have to assume that it was deliberate that you did that, that you want to be accessing two different relative indices, and that your only mistake for the Listener Callback is in displaying them both to the same axes (because the target axes had not been specified for the second imshow())
If I were writing code that had to slide through a single data source, I would use only the Callback and not the listener at all. But I have to assume you had good reason for using both with different functionality.
Stelios Fanourakis
on 4 Jun 2018
Walter Roberson
on 4 Jun 2018
As I explained, that is because you are asking to display according to frameindex, but frameindex is only changed by the slider Callback; your Listener callback changes CurrentFrame not frameindex. It happens to be the case that when you have both a slider Callback and a Listener for that slider, that the slider Callback will be called the first time but after that as long as the slider is what has focus, the listener callback will be called instead.
The one on your left is displaying information from your Matrix variable. The right one is displaying information read in from files. Your code was displaying both, one on top of the other, which resulted in the second one erasing the first.
I do not know why you are maintaining Matrix separate from reading from a file, and I do not know why you programmed a Slider callback separate from a Listener callback. If you can explain why you programmed Callback and Listener as well, and if you can explain the difference for your purposes between CurrentFrame and frameindex, and if you can explain the difference between the Matrix variable and reading from the file, then I might be able to get further.
I cannot repair your code until you explain what your intentions were in creating the code that way.
You must have had a reason for having frameindex separate from CurrentFrame ??
Stelios Fanourakis
on 5 Jun 2018
Walter Roberson
on 5 Jun 2018
I'm sorry, you will need to get someone else to assist you in understanding what you want. When that happens, they can contact me for the technical details of how to achieve that specific thing.
Stelios Fanourakis
on 5 Jun 2018
Stelios Fanourakis
on 5 Jun 2018
Categories
Find more on Display 2-D Images 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!