1

Because I am trying to let a GUI element slice my array, there will be a : (colon) sign in the variables. This returns me an error:

Error in gui_mainfcn (line 96)
         feval(varargin{:});

line 96 refers to this code:

image(handles.data(1:handles.rows,1:handles.cols, temp))

Temp looks like this

temp = 

1    1    1    1    2    1    1    1    1

And both handles.rows and cols are the value 64. So the problem seems to be that I use colons in the gui function. However, to slice I need to use colons. My question now is: Any idea how to work around this?

To clarify as requested below

The above code works when I manually enter it in the console. Also when I use handles.data(:,:,1,1,1,1,2,1,1,1,1), handles.data(1:end,1:end,1,1,1,1,2,1,1,1,1), handles.data(1:64,1:64,1,1,1,1,2,1,1,1,1), etc I get the same error from the gui. Manually they all work and return a 64 by 64 array of doubles which I can plot with image().

Might be related to these questions, however those deal with parfor difficulties and dont seem to answer my question:

matlab-parfor-slicing-issue

index-inside-parfor-slicing

I am now also reading the advanced topics for slicing variables. Still dont see what I am doing wrong though, so any help or explanation would still be greatly apprectiated. Thanks!

Community
  • 1
  • 1
Leo
  • 1,757
  • 3
  • 20
  • 44
  • Do you happen to see the text `Function 'subsindex' is not defined for values of class 'cell'.` anywhere near the error message? Your `temp` variable is a cell, so you can't index handles.data with it. – chappjc Sep 16 '13 at 18:21
  • Ah yes, sorry I posted the old form of temp. I got that error before, but not anymore. Thanks anyways. I corrected my code in the question. – Leo Sep 16 '13 at 18:29

2 Answers2

1

Explanation

By putting the vector temp as the third index into your data, you are not indexing the higher dimensions - you are repeatedly indexing the third. In other words, you get handles.data(:,:,[1 1 1 1 2 1 1 1 1]) instead of handles.data(:,:,1,1,1,1,2,1,1,1,1).

Solution

Here's a solution that doesn't require squeeze or eval. It exploits the comma-separated lists output of the {:} syntax with cell arrays, and the ability to apply linear indexing on the last subscripted dimension.

ctemp = num2cell(temp); % put each index into a cell
sz = size(handles.data); % i.e. sz = [256 256 1 1 2 1 2]
sliceind = sub2ind(sz(3:end),ctemp{:}); % compute high dim. linear index (scalar)
image(handles.data(:,:,sliceind));

This performs subscripting of a >3D array with only 3 subscripts by computing the last subscript as a linear index. It's weird, but convenient sometimes.

chappjc
  • 30,359
  • 6
  • 75
  • 132
  • My slicing creates a 64 by 64 array of doubles. Manually entering the code works. However the gui cant process data(:,:,1,1,1,1) or data(1:end,1:end,1,1,1,1) or data(1:64,1:64,1,1,1,1) etc. – Leo Sep 16 '13 at 18:40
  • I'm not sure what you want to do, but each comma goes to the next dimension. What is `size(data)`? – chappjc Sep 16 '13 at 18:50
  • yes each comma goes to the next dimension, but the only dimensions in the resulting slice are those with more than one value in the index. The ranges create the dimension in the slice, the single numbers are the position from which the ranges are taken. 1:64,1:64 is like saying take all pixels, and then all the ones and twos just point to the dimension from which to take those pixels. – Leo Sep 17 '13 at 07:20
  • Sorry, to answer your question as well: size(data) = 64 64 1 1 1 1 2 1 1 1 1; size(data(:,:,1,1,1,1,2,1,1,1,1)) = 64 64; – Leo Sep 17 '13 at 08:18
  • See my updated answer. The final answer would be even easier if you can always rely on dimensions 1, 2 and 7 being the only non-singleton dimensions. Is that the case? – chappjc Sep 17 '13 at 15:41
1

A heads up for people with the same problem, this error can not only result from not knowing how to slice, it could also result from not having defined your variables correctly: http://www.mathworks.nl/matlabcentral/answers/87417-how-to-slice-inside-gui-without-error-feval-varargin

Leo
  • 1,757
  • 3
  • 20
  • 44