如何在函数的输出上使用索引? [英] How can I use indexing on the output of a function?
问题描述
Possible Duplicate:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
我想在函数的输出上使用索引.我使用textscan函数读取非常大的文本文件(15 GB).在我的情况下,textscan函数的返回值是一个1x1的单元格数组,其中包含一个非常大的数字数组.
I would like to use indexing on the output of a function. I use the textscan function to read very large text files (15 GB). The return of the textscan function in my case is a 1x1 cell array that contains a very large numeric array.
代替:
tmp = textscan(...);
final_result = mat2cell(tmp{1,1});
我想这样做:
final_result = mat2cell( textscan(...){1,1} );
如果这行得通,它将避免创建非常大的临时变量tmp.还有另一种避免临时变量的方法吗?
If this would work, it would avoid the creation of the very large temporary variable tmp. Is there another way to avoid the temporary variable?
推荐答案
如果您仍然想知道,请考虑以下示例:
In case you are still wondering, consider this example:
%# some function that returns a cell array (TEXTSCAN in your case)
myFunc = @() {rand(5,5)};
%# normally you would write
C = myFunc();
C = C{1,1};
Here is the cellarray-version of @gnovice answer in the linked question (ugly but works):
%# equivalent to: C = myFunc(){1,1}
C = subsref(myFunc(), struct('type','{}','subs',{{[1 1]}}))
这篇关于如何在函数的输出上使用索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!