이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How to assign each axes to hold/keep its own image?
조회 수: 2 (최근 30일)
이전 댓글 표시
Stelios Fanourakis
2019년 10월 8일
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
채택된 답변
Rik
2019년 10월 8일
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
Stelios Fanourakis
2019년 10월 8일
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
2019년 10월 8일
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
2019년 10월 9일
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
2019년 10월 9일
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
2019년 10월 9일
편집: Rik
2019년 10월 9일
Is this what you mean by direct handle to an image??
i3 = imshow(squeeze(im4(:,:,slice1,:)),'XData',[1 592], 'YData',[1 481]);
Stelios Fanourakis
2019년 10월 9일
편집: Rik
2019년 10월 9일
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
2019년 10월 9일
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
2019년 10월 9일
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
2019년 10월 9일
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
2019년 10월 9일
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
2019년 10월 9일
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
2019년 10월 9일
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
2019년 10월 9일
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
2019년 10월 9일
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
2019년 10월 9일
Where did you define sno? Because if that is higher than size(Img,3) it will cause this error.
Stelios Fanourakis
2019년 10월 9일
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
2019년 10월 9일
편집: Stelios Fanourakis
2019년 10월 9일
@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
2019년 10월 9일
편집: Rik
2019년 10월 9일
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
2019년 10월 9일
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
2019년 10월 10일
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
2019년 10월 10일
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
2019년 10월 10일
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
2019년 10월 10일
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
2019년 10월 10일
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
2019년 10월 10일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기
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 (한국어)