Subsref question using braces '{}' type and a char subs

Hello, I am implementing subsref in my class for dealing with braces, '{}'. I want to be able to call
>> classname{'chars'}
but all I get is an error:
??? Error using ==> subsref
Too many output arguments.
The debugger never even gets into the subsref function for me to see whats happening. If I use the same logic but for '()' type indexing, it works fine. Similarly, if I use '{}' but with a number I can get the code to to into the subsref, but I need to be able to use '{}' with char subs not a number/index.
Here is the switch case for the '{}' type
switch s(1).type
case '{}'
v = obj.variable(s(1).subs);
if length(s) == 2
B = v.data(s(2).subs);
elseif length(s) == 3
switch s(3).subs
case data
B = v.data(s(2).subs);
case grid
B = v.grid(s(2).subs);
end
else
B = v;
end

10 件のコメント

Walter Roberson
Walter Roberson 2011 年 2 月 25 日
Question: are data and grid defined variables? If not then perhaps they should be coded as 'data' or perhaps even {{'data'}} ? (If they _are_ defined variables then you have a name conflict with the function grid())
Alexander
Alexander 2011 年 2 月 28 日
data and grid are methods in the class, and when grid is called from the class matlab uses the method found in the class not matlab's builtin function
Walter Roberson
Walter Roberson 2011 年 2 月 28 日
Note that when you code
case data
then Matlab will evaluate data and try to match the result against s(3).subs . Is that what you want, or in that context are you intending data to be a constant such as 'data' ?
Alexander
Alexander 2011 年 2 月 28 日
Oh you're right i left off the single quotes, Thanks!
Alexander
Alexander 2011 年 2 月 28 日
To the point though: Why can't I even get the code to enter the subsref code if I use characters in braces, instead of numbers (which do allow the code to enter subsref).
case '{}'
v = obj.variable(s(1).subs);
B = v;
Walter Roberson
Walter Roberson 2011 年 2 月 28 日
I speculate that possibly you might find something interesting if you put a default case in for the type and display the type there.
Alexander
Alexander 2011 年 2 月 28 日
Can you elaborate? I tried replacing the braces with parenthesis and it seemed to work fine.
Alexander
Alexander 2011 年 2 月 28 日
Similarly, I can construct a subsref to interpet
>> classname.data{'chars'}
and it works just fine.
Walter Roberson
Walter Roberson 2011 年 2 月 28 日
I am hypothesizing that for some reason the type is not matching '{}'
Perhaps I have misunderstood. Are you indicating that you have demonstrated that it never reaches the switch s(1).type code? Or is it possible that that switch is executed but '{}' is not what is in s(1).type ?
Alexander
Alexander 2011 年 2 月 28 日
It never reaches the switch s(1).type code. In fact it gives the error above without entering subsref at all. If I put the command into profiler, I don't get any profiling results, just the error.

サインインしてコメントする。

 採用された回答

Philip Borghesani
Philip Borghesani 2011 年 3 月 1 日

1 投票

Your class needs a NUMEL function. A string is a matrix in MATLAB so classname{'foo'} is the same as calling classname{double('foo')}. Adding this function to your class will show you what is going on:
function num=numel(obj,varargin)
fprintf('NUMEL called with %d indices',nargin-1);
num=builtin('numel',obj,varargin{:});
fprintf(' NUMEL returned %d\n',num);
end
To make string inputs work the way you want have NUMEL return 1 or prod(size(obj)) for char type inputs depending on what behavior is desired.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCustomize Object Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by