Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

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

Asked by Alexander on 25 Feb 2011

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 Comments

Alexander on 28 Feb 2011

Similarly, I can construct a subsref to interpet
>> classname.data{'chars'}
and it works just fine.

Walter Roberson on 28 Feb 2011

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 on 28 Feb 2011

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.

Alexander

Products

No products are associated with this question.

1 Answer

Answer by Philip Borghesani on 1 Mar 2011
Accepted answer

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 Comments

Philip Borghesani

Contact us