如何使用MATLAB的substruct函数创建表示使用&"end&"的引用的结构? [英] How do I use MATLAB's substruct function to create a struct representing a reference that uses "end"?
问题描述
我想使用 substruct
函数来创建要在 subsref
中使用的结构.目的是使用 subsref
而不是通常的()
标记来索引字符串,因为我正在对函数的输出进行下标.这是我要执行的操作的简单示例(在我的实际代码中,此示例在 cellfun
中使用,因此字符串的长度可以不同,并且替换并不总是在同一地点):
I'd like to use the substruct
function to create a structure for use in subsref
. The purpose is to index a string using subsref
instead of the usual ( )
notation because I'm subscripting the output of a function. This is a simple example of what I'm trying to do (in my actual code, this is used within a cellfun
, so the strings can be of different lengths and the replacement isn't always in the same place):
data = 'quick brown fox';
data2 = strrep(data, 'brown', 'green');
data2(7:end)
在这里我尝试使用 substruct
定义此下标:
Heres where I tried to define this subscripting with substruct
:
data = 'quick brown fox';
S = substruct('()', {[7:end]});
subsref(strrep(data, 'brown', 'green'), S)
但这给我一个错误:
Error using substruct (line 30)
SUBSTRUCT takes at least two arguments.
Error in myfile (line 3)
S = substruct('()', {(7:end)});
我已经遍历了 substruct
和 subsref
的文档,没有人提到 end
.我该怎么做?
I've been over and over the documentation for both substruct
and subsref
, and no where do they mention end
. How do I do this?
推荐答案
据我所知,没有办法使用 subsref
做到这一点,假定您确切知道哪些 subs
您将需要.
There is no way to my knowledge to do that with subsref
which assumes you know exactly which subs
you will need.
使用 @function_handle
动态确定 end
:
f = @(x) x(7:end);
f(strrep(data, 'brown', ''))
ans =
fox
这篇关于如何使用MATLAB的substruct函数创建表示使用&"end&"的引用的结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!