Using slice in a fuction and from command window

6 views (last 30 days)
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 Comments
Selva Karna
Selva Karna on 17 Aug 2017
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
Sergio  Morales
Sergio Morales on 17 Aug 2017
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

Sign in to comment.

Accepted Answer

Adam
Adam on 17 Aug 2017
Edited: Adam on 18 Aug 2017
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.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!