Why slider not working?
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2018-6-1
编辑:Walter Roberson
2018-6-1
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
2018-6-2
im2,S and sno parameters are defined at load image Callback function which is actually a push button on the interface.
Stelios Fanourakis
2018-6-3
I have this code:
function SliderDemo
clc
filelist=dir(fullfile('D:\stelios phd files\DesMoines\karadokei\New\*.dcm'));
handles.filelist = filelist;
handles.frameindex = 1;
NumFrames = 15; %// Check below for dummy 4D matrix/image sequence
hFig = figure('Position',[100 100 500 500],'Units','normalized');
handles.axes1 = axes('Units','normalized','Position',[.2 .2 .6 .6]);
%// Create slider and listener object for smooth visualization
handles.SliderFrame = uicontrol('Style','slider','Position',[60 20 400 50],'Min',1,'Max',NumFrames,'Value',1,'SliderStep',[1/NumFrames 2/NumFrames],'Callback',@XSliderCallback);
handles.SliderxListener = addlistener(handles.SliderFrame,'Value','PostSet',@XListenerCallBack);
handles.Text1 = uicontrol('Style','Text','Position',[180 420 60 30],'String','Current frame');
handles.Edit1 = uicontrol('Style','Edit','Position',[250 420 100 30],'String','1');
%// Create dummy image sequence, here 4D sequence of grayscale images.
Img = DicomReader('S0000009.dcm')
isoImage = makeImIsoRGB(Img, [1,1,15], 1.5, 'cubic')
MyImage = isoImage;
MyMatrix = MyImage;
%// Use setappdata to store the image stack and in callbacks, use getappdata to retrieve it and use it. Check the docs for the calling syntax.
setappdata(hFig,'MyMatrix',MyMatrix); %// You could use %//setappdata(0,'MyMatrix',MyMatrix) to store in the base workspace.
%// Display 1st frame
imshow(MyMatrix(:,:,1),[])
%// IMPORTANT. Update handles structure.
guidata(hFig,handles);
%// Listener callback, executed when you drag the slider.
%// Slider callback; executed when the slider is release or you press
%// the arrows.
function XSliderCallback(~,~)
handles = guidata(gcf);
%// Here retrieve MyMatrix using getappdata.
MyMatrix = getappdata(hFig,'MyMatrix');
CurrentFrame = round((get(handles.SliderFrame,'Value')));
set(handles.Edit1,'String',num2str(CurrentFrame));
imshow(MyMatrix(:,:,CurrentFrame),'parent',handles.axes1);
filelist = handles.filelist;
frameindex = handles.frameindex;
myfolder='D:\stelios phd files\DesMoines\karadokei\New';
currentframefile = filelist(frameindex).name;
handles.frameindex = frameindex+1;
ff=fullfile(myfolder,currentframefile);
I=dicomread(ff);
imshow(I);
guidata(hFig,handles);
end
end
It perfectly works, only once. Untill the left slider bar reaches the right end. When I try to reslide it again backwards, from right to left, it becomes a mess and get errors like
Warning: Error executing listener callback for PostSet event on Value dynamic property in object of
matlab.ui.control.UIControl class:
Undefined function 'XListenerCallBack' for input arguments of type 'matlab.graphics.internal.GraphicsMetaProperty'.
Index exceeds matrix dimensions.
Error in SliderDemo/XSliderCallback (line 60)
currentframefile = filelist(frameindex).name;
Error while evaluating UIControl Callback.
Please, help.
Walter Roberson
2018-6-3
When the problem occurs, what value does frameindex end up having, and what size is filelist ?
Stelios Fanourakis
2018-6-3
Walter please read my previous comment. The problem occurs when I try to slide backwards the slider, from right to end left. frameindex = 1; and filelist = dir(fullfile('D:\stelios phd files\DesMoines\karadokei\New\*.dcm'));
Can you please help me have a slider that works both sides and if possible give me a code to run slider with the mouse scrolling wheel
Walter Roberson
2018-6-3
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
2018-6-3
Well. When the slider finishes at the end right and start to drag it back to left. I get the error at the first left click and the values are, CurrentFrame: 14 frameindex: 16.
Should they be equal? What to do to change it/?
回答(1 个)
Walter Roberson
2018-6-2
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 个评论
Walter Roberson
2018-6-3
frameindex = max(1, min(handles.frameindex, NumFrames) );
also I recommend testing NumFrames > 0 just in case.
Stelios Fanourakis
2018-6-3
Although it does move back the slider now I get another error.
Warning: Error executing listener callback for PostSet event on Value dynamic property in object of
matlab.ui.control.UIControl class:
Error using SliderDemo/XListenerCallBack
Too many input arguments.
Warning: Error executing listener callback for PostSet event on Value dynamic property in object of
matlab.ui.control.UIControl class:
Error using SliderDemo/XListenerCallBack
Too many input arguments.
And the images do not change backwards.
Walter Roberson
2018-6-3
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
2018-6-3
Actually it only works once. From left to right. When I try to repeat it, it fails to change the images although the slider works normaly
Walter Roberson
2018-6-3
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
2018-6-3
I send you anything you need in this zip. You have the functions and the images. See that the slider works only once. Waiting to your divine enlighten solution.
Walter Roberson
2018-6-3
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
2018-6-4
the first imshow is not working as I'd like. It dims the images, it doesn't change them. Second imshow works but as I told you, only for once. When I try to repeat the process it fails
Walter Roberson
2018-6-4
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
2018-6-4
Dear Walter.
Thank you very much for your nice contribution. Although, it still do not help me much. The code you gave me shows two axes. The left one is the original which actually dims the same image. Not what I want to do.
The right axes, changes the images, only once, when the slider goes from left to right. When I try to reslide it back to left it does not do anything.
Walter Roberson
2018-6-4
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
2018-6-5
Dear Walter. I cannot understand why you are making me that questions. The code actually works and the slider change images. The problem is why it works only for a couple of times the most and then stops.
I removed listener and kept only slider, it doesn't make any difference. Matlab keeps showing the last imshow I put in the SliderCallback function. I put both CurrentFrame and frameindex. None of those methods solve the problem of functionality.
It has to do something with the callback. Maybe the uicontrol function. It works for only a couple of times. Why not working all the time?
Walter Roberson
2018-6-5
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
2018-6-5
I know what I want. I explained to my previous comment. If you can give me a code that actually changes images forward and backwards, I'll accept your answer
Stelios Fanourakis
2018-6-5
Can you please let me know how can I make this line
CurrentFrame = round((get(handles.SliderFrame,'Value')));
To actually work and change between frames and not just dim the first image.
另请参阅
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 (한국어)