現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How to assign each axes to hold/keep its own image?
1 回表示 (過去 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 で Specifying Target for Graphics Output についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)