Using slice in a fuction and from command window

Hi everyone: I am using slice to display views of a Ultrasound created image volume. When I run it from the comman window I get the error of "Index exceeds matrix dimensions" but if I create a function in which I call slice and produce the cuts then it works. I have tried with simple examples like the one included in the description of the slice function usage and I get the same results. Being a the volume with dimensions (149x148x200):
-------------------------------------------
Command Window: sizevol=size(a);
% %slice and visualise cross sections for volume
[xgrid,ygrid,zgrid]=meshgrid(1:sizevol(2),1:sizevol(1),1:sizevol(3));
a=double(a);
h=slice(xgrid,ygrid,zgrid,a,100,100,10);
ERROR - Index exceeds matrix dimensions
-------------------------------------------
Function: slicefcn(a);
"""""
function slicefnc(V)
figure;
[x,y,z] = meshgrid(1:size(V,2),1:size(V,1),1:size(V,3));
hslices=slice(x,y,z,V,size(V,2)/2,size(V,1)/2,size(V,3)/2);%
colormap(gray);
xlabel('x');ylabel('y');zlabel('z');
end
""""
getting the desired result attached;
Has anyone an idea why this happens? am I missing or ignoring something? Cheers
Sergiusz

댓글 수: 2

V1=magic(5); V(:,:,1)=V1 V(:,:,2)=V1 V(:,:,3)=V1 slicefnc(V)
function slicefnc(V) figure; [x,y,z] = meshgrid(1:size(V,2),1:size(V,1),1:size(V,3)); hslices=slice(x,y,z,V,size(V,2)/2,size(V,1)/2,size(V,3)/2);% colormap(gray); xlabel('x');ylabel('y');zlabel('z'); end
I restarted matlab and it worked, no idea why. This time i used the flag -softwareopengl although I dont think that's the reason. :/ Txs for your answer

댓글을 달려면 로그인하십시오.

 채택된 답변

Adam
Adam 2017년 8월 17일
편집: Adam 2017년 8월 18일

1 개 추천

Almost certainly you had a variable in your workspace called slice which hides the function. The error message gives a big hint to this when you remember that the syntax is the same to index into a variable and to pass arguments to a function.
If slice is a variable then
slice( 1, 2, 3 )
tries to index into a 3d array with indices 1, 2 and 3 in the three dimensions. If this is inappropriate because your 'slice' variable does not have the right dimensionality or size then you will get an error message like the one you see.
That error message:
'Index exceeds matrix dimensions'
will, to my knowledge, never point at a function call (it may point to a line within a function, but not to the function call line itself as the top-most line of the call stack) so when you get this error message for what you expect to be a function the first thing you should always check is that you haven't overridden the function with a variable.
As an aside, typing
which slice
on the command line before naming a variable 'slice' is good. If it reports a function then find another name for your variable because you are about to hide that function.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2017년 8월 17일

편집:

2017년 8월 18일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by