You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to assign each axes to hold/keep its own image?
1 view (last 30 days)
Show older comments
Hi
I have this set of lines
set(get(gca,'children'),'cdata',squeeze(Img(:,:,S,:))); %% For Axes 1
set(get(gca,'children'),'cdata',squeeze(Img2(:,:,S2,:))) %% For Axes 2
set(get(gca,'children'),'cdata',squeeze(Img3(:,:,S3,:))) %% For Axes 3
But because of the hierarchy sequence I only get the Img3 at all axes. When I click upon each one of the axes I get the last image I assigned on this set of lines. How do I make it to keep its image for every each axes seperately? So Axes 1 should have Img, Axes 2 Img2 and Axes 3 Img3
Accepted Answer
Rik
on 8 Oct 2019
You should never use gcf or gca in code for a GUI. As your tags indicate you're using GUIDE: use the explicit handles to the axes object stored in the handles struct.
27 Comments
Stelios Fanourakis
on 8 Oct 2019
So you say to try something like that
set(get(handles.axes1,'children'),'cdata',squeeze(Img(:,:,S,:))); %%For Axes 1
set(get(handles.axe2,'children'),'cdata',squeeze(Img2(:,:,S2,:))); %%For Axes 2
set(get(handles.axes3,'children'),'cdata',squeeze(Img4(:,:,S3,:))); %%For Axes 3
I am going to try and let you know the outcome.
Rik
on 8 Oct 2019
Or even better: replace the get(handles.axes1,'children') by the direct handle to the image object (which is returned by the function that created it).
Stelios Fanourakis
on 9 Oct 2019
I fixed the 2 axes but the first one remained faulty and now I get the error
Error using matlab.graphics.primitive.Data/set
There is no cdata property on the Line class.
Regarding this line
set(get(handles.axes1,'children'),'cdata',squeeze(Img(:,:,S,:))); %%For Axes 1
Rik
on 9 Oct 2019
And that is why I suggested using the direct handle. Apparently the axes has a line object as a child (as well?). If you want to change the CData of the image object you should store the handle to that object in the guidata struct.
Stelios Fanourakis
on 9 Oct 2019
Edited: Rik
on 9 Oct 2019
Is this what you mean by direct handle to an image??
i3 = imshow(squeeze(im4(:,:,slice1,:)),'XData',[1 592], 'YData',[1 481]);
Stelios Fanourakis
on 9 Oct 2019
Edited: Rik
on 9 Oct 2019
I tried that solution and it works. However, when I drag the slider manually and then try again to move it by scrolling the wheel I get the error
Error using matlab.graphics.primitive.Image/set
Invalid or deleted object.
Error in experiment/mouseScroll (line 271)
set(i1,'cdata',squeeze(im2(:,:,S,:))); %%For Axes 1
Error using experiment/XSliderCallback (line 217)
Error while evaluating Figure WindowScrollWheelFcn.
Why this error?
Rik
on 9 Oct 2019
That is indeed what I mean. I would try to find the cause of the image object being deleted. It is also a smart move to include the parent axes in your call to imshow (or image) so they don't have to implicitly call gca, which might overwrite things in your axes (especially in the case of imshow, which does a lot of housekeeping for you).
Stelios Fanourakis
on 9 Oct 2019
Can you please give me an example? Something like that?
i3 = imshow(squeeze(im4(:,:,slice1,:)),'XData',[1 592], 'YData',[1 481], 'parent' handles.axes3);
Stelios Fanourakis
on 9 Oct 2019
How can I find the cause of the image object being deleted? Is there a specific procedure since the error message is a bit vague.
Stelios Fanourakis
on 9 Oct 2019
Now it's not working and I get the error
Index exceeds matrix dimensions.
Error in experiment/mouseScroll (line 273)
set(i1,'cdata',squeeze(im2(:,:,S,:))); %%For Axes 1
Error while evaluating Figure WindowScrollWheelFcn.
Rik
on 9 Oct 2019
Almost: you're missing a comma here.
i3 = imshow(squeeze(im4(:,:,slice1,:)),'XData',[1 592], 'YData',[1 481], 'parent',handles.axes3);
% ----------------------^
And the best way to find where an object is being deleted is to step through the code. You can also try setting the DeleteFcn:
i3 = imshow(squeeze(im4(:,:,slice1,:)),'XData',[1 592], 'YData',[1 481], 'parent',handles.axes3);
set(i3,'DeleteFcn','error(''object is being deleted'')')
Now you can set the stop on error debugging option to find out when the object is being deleted.
Stelios Fanourakis
on 9 Oct 2019
Now I get the error
Index exceeds matrix dimensions.
Error in experiment/mouseScroll (line 273)
set(i1,'cdata',MyMatrix(:,:,S,:)); %%For Axes 1
Error while evaluating Figure WindowScrollWheelFcn.
Any idea why Index could exceed matrix dimensions?
Rik
on 9 Oct 2019
How did you code the value of S? Are you making sure it is capped to the size of the dimension that changing?
Stelios Fanourakis
on 9 Oct 2019
S stands for the slices that needs to be changed at every sliding
S = round((get(handles.SliderFrame1,'Value')));
S2 = round((get(handles.SliderFrame2,'Value')));
S3 = round((get(handles.SliderFrame3,'Value')));
UPDN = eventdata.VerticalScrollCount;
S = S - UPDN;
S2 = S2 - UPDN;
S3 = S3 - UPDN;
if (S < 1) | (S2 < 1) | (S3 < 1)
S = 1;
S2 = 1;
S3 = 1;
elseif (S > sno) | (S2 > sno) | (S3 > sno)
S = sno;
S2 = sno;
S3 = sno;
set(handles.SliderFrame1,'Value',S);
set(handles.SliderFrame2,'Value',S2);
set(handles.SliderFrame3,'Value',S3);
%% where sno = number of slices
After that script follows the one with the error
set(i1,'cdata',squeeze(Img(:,:,S,:))); %%For Axes 1
set(i2,'cdata',squeeze(Img2(:,:,S2,:))); %%For Axes 2
set(i3,'cdata',squeeze(Img4(:,:,S3,:))); %%For Axes 3
Rik
on 9 Oct 2019
Where did you define sno? Because if that is higher than size(Img,3) it will cause this error.
Stelios Fanourakis
on 9 Oct 2019
This Index Exceeds Matrix Dimensions is a new error. How can I trace its origin? I don't know but with the same commands it used to run.
Stelios Fanourakis
on 9 Oct 2019
Edited: Stelios Fanourakis
on 9 Oct 2019
@Rik, sno = size(im2) It is defined at the begining of the script making it global value
Why it shouldn't be higher than the number of slices (Img,3)
Stelios Fanourakis
on 9 Oct 2019
Edited: Rik
on 9 Oct 2019
I append the whole mouse scroll function here. Hope you can help me.
function mouseScroll(~,eventdata,I)
handles = guidata(gcf);
S = round((get(handles.SliderFrame1,'Value')));
S2 = round((get(handles.SliderFrame2,'Value')));
S3 = round((get(handles.SliderFrame3,'Value')));
sno = size(MyMatrix);
UPDN = eventdata.VerticalScrollCount;
S = S - UPDN;
S2 = S2 - UPDN;
S3 = S3 - UPDN;
if (S < 1) | (S2 < 1) | (S3 < 1)
S = 1;
S2 = 1;
S3 = 1;
elseif (S > sno) | (S2 > sno) | (S3 > sno)
S = sno;
S2 = sno;
S3 = sno;
global S S2 S3
set(handles.SliderFrame1,'Value',S);
set(handles.SliderFrame2,'Value',S2);
set(handles.SliderFrame3,'Value',S3);
set(handles.Edit1, 'String', sprintf('Slice# %d / %d',S, sno));
else
set(handles.Edit1, 'String', '2D image');
end
set(i1,'cdata',squeeze(im2(:,:,S,:))); %%For Axes 1
set(i2,'cdata',squeeze(im3(:,:,S2,:))); %%For Axes 2
set(i3,'cdata',squeeze(im4(:,:,S3,:))); %%For Axes 3
end
Where MyMatrix = im2 and im3 and im4 are actually the same image under permutation. But the sizes are the same
Rik
on 9 Oct 2019
Consider this example:
MyMatrix=rand(100,120,40);
S=40;
sno=size(MyMatrix)
%now your callback runs:
S=S+1;
cond=(S > sno) | (S2 > sno) | (S3 > sno);
clc,disp(cond)
What do you think if will do when you input your conditional? If you aren't 100% sure, it is probably not what you think.
You should always make sure to have a scalar input to if and while. In your case I would suggest something like this:
function mouseScroll(hObject,~,I)
%you aren't using I, and with GUIDE the third input is usually 'handles'
handles = guidata(hObject);
im2=handles.im2;i1=handles.i1;
im3=handles.im3;i2=handles.i2;
im4=handles.im4;i3=handles.i3;
S(1) = round((get(handles.SliderFrame1,'Value')));
S(2) = round((get(handles.SliderFrame2,'Value')));
S(3) = round((get(handles.SliderFrame3,'Value')));
UPDN = eventdata.VerticalScrollCount;
S=S-UPDN;
S=max([1 1 1],S);%ensure S is at least 1
S=min(size(im2),S);%ensure S is at most size(im2,___)
set(handles.SliderFrame1,'Value',S(1));
set(handles.SliderFrame2,'Value',S(2));
set(handles.SliderFrame3,'Value',S(3));
set(handles.Edit1, 'String', sprintf('Slice# %d / %d',S(3), sno(3)));
set(i1,'cdata',squeeze(im2(:,:,S(1),:))); %%For Axes 1
set(i2,'cdata',squeeze(im3(:,:,S(2),:))); %%For Axes 2
set(i3,'cdata',squeeze(im4(:,:,S(3),:))); %%For Axes 3
end
Stelios Fanourakis
on 10 Oct 2019
I also get this error
Error in experiment/mouseScroll (line 243)
im2=handles.im2;i1=handles.i1;
Error while evaluating Figure WindowScrollWheelFcn.
Reference to non-existent field 'im2'.
Rik
on 10 Oct 2019
You should be able to find the cause of this error on your own. The code is attempting to assign a value to the variable im2, and it tries to do that by extracting a field from the handles struct. Apparently that field doesn't exist.
When writing this code, I assumed you had it stored in the handles struct, because that makes the most sense. Replace this code with whatever name you have stored your image in the struct.
Stelios Fanourakis
on 10 Oct 2019
After renaming everything using handles.im2,handles.im3 and so on so forth I get a new error
Undefined variable "eventdata" or class "eventdata.VerticalScrollCount".
Error in experiment/mouseScroll (line 251)
UPDN = eventdata.VerticalScrollCount;
Error while evaluating Figure WindowScrollWheelFcn.
I didn't have that error previously
Rik
on 10 Oct 2019
Ah, that one is on me, I edit the function header without checking if you were indeed not using eventdata (since I hardly ever use it).
function mouseScroll(hObject,~,I)
%change to
function mouseScroll(hObject,eventdata,I)
You should also confirm that you haven't changed the callback. Because if you haven't, the third input in a GUIDE-generated GUI is the handles struct.
Stelios Fanourakis
on 10 Oct 2019
Dear RIk
Thank you so much for your valuable contribution. My Viewer seems to work at the moment.
However the mouse scrolling stops only at the left end whereas at the right end of the slider I get the error
Warning: 'slider' control cannot have a 'Value' outside of 'Min'-'Max' range
Control will not be rendered until all of its parameter values are valid
Index exceeds matrix dimensions.
Error in experiment/mouseScroll (line 259)
set(i1,'cdata',squeeze(im2(:,:,S(1),:))); %%For Axes 1
Error while evaluating Figure WindowScrollWheelFcn.
Rik
on 10 Oct 2019
Use the debugger. Follow the flow of your program. Find out where the properties of your slider are set. Find out when the results are different from what you expect.
The warning you see tells you that some code is setting the value property to something bigger than max. And then you get error that tells you your index is too big. That means either my code doesn't work, or the size is not retrieved correctly. Check both options.
More Answers (0)
See Also
Categories
Find more on Specifying Target for Graphics Output 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 (한국어)