1

因为我试图让 GUI 元素对我的数组进行切片,所以变量中会有一个 :(冒号)符号。这给我一个错误:

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

第 96 行引用此代码:

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

温度看起来像这样

temp = 

1    1    1    1    2    1    1    1    1

而且handles.rows 和cols 的值都是64。所以问题似乎是我在gui 函数中使用了冒号。但是,要切片,我需要使用冒号。我现在的问题是:知道如何解决这个问题吗?

澄清如下要求

当我在控制台中手动输入时,上面的代码有效。另外当我使用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) 等我得到同样的错误gui。手动它们都可以工作并返回一个 64 x 64 的双精度数组,我可以用 image() 绘制它。

可能与这些问题有关,但是那些处理parfor困难并且似乎没有回答我的问题:

matlab-parfor-切片问题

分片内索引

我现在也在阅读 切片变量的高级主题 。仍然没有看到我做错了什么,所以任何帮助或解释仍然会非常感激。谢谢!

4

2 回答 2

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.

于 2013-09-16T18:33:36.010 回答
1

提醒有同样问题的人,这个错误不仅可能是由于不知道如何切片,还可能是由于没有正确定义变量:http: //www.mathworks.nl/matlabcentral/answers/87417 -how-to-slice-inside-gui-without-error-feval-varargin

于 2013-10-01T06:52:56.783 回答