1

I am having trouble iterating through a cell array of strings which is a constant property in a class. Every time I try to index the cell array from 1 to end, Matlab crashes. What could I be doing wrong?

Code looks like this:

classdef classA
    properties(Constant)
        ValidElements = {'Elem1', 'Elem2', 'Elem3'};
    end
    properties
        AInfo
    end
    function this = classA(info)
        1stvalidelement = classA.ValidElements{1};
        validelementslist = classA.ValidElements{1:end};
        ...
        ...
        this.AInfo = info;
    end
    ...
end

Inside the constructor, 1st line executes well without any problem but at the 2nd line matlab crashes. I am totally clueless as to why this could be. Please help me.

Thanks.

chappjc
  • 30,359
  • 6
  • 75
  • 132
DEEPMALA
  • 53
  • 1
  • 1
  • 7
  • "MATLAB crashes"...as in: MATLAB exits without warnings or errors? You get segfaults? What errors do you see? – Rody Oldenhuis Feb 07 '14 at 08:17
  • I see access violation error, but same thing works when ValidElements is a local variable and I am trying to access it. I mean no access violation error. – DEEPMALA Feb 07 '14 at 08:23
  • Also, I noticed that whenever I am indexing using numbers like Block.ValidElements{1:2} its not crashing but as soon as I start using Block.ValidElements{1:end} it crashes. Weird!!! But everytime it returns only the value of the 1st cell-element. – DEEPMALA Feb 07 '14 at 08:31
  • What MATLAB version are you using? – Rody Oldenhuis Feb 07 '14 at 11:51

1 Answers1

1

Well, for one thing, you are using curly braces where you (probably) mean to use parenthesis.

Compare

>> A = {'one' 'two' 'three'}';
>> A{1:2}
    ans =
        one
    ans =
        two
>> A(2:3)
    ans = 
        'two'
        'three'

The difference is that parenthsis (()) will return part of the cell as a new cell, while curlies ({}) will return the entries of the cell as a (comma-separated) list. Curly braces therefore return multiple values in this case, which you try to assign to a single variable.

For another thing, the function accessing the property is not in a (Static) method block.

And another thing, 1stvalidelement is not a valid variable name.

Therefore:

classdef classA
    properties(Constant)
        ValidElements = {'Elem1', 'Elem2', 'Elem3'};
    end
    properties
        AInfo
    end

    method (Static)

        function this = classA(info)
            firstvalidelement = classA.ValidElements{1};
            validelementslist = classA.ValidElements(1:end);
            ...
            ...
            this.AInfo = info;
        end

        ...

    end
end
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • Actually, that statement was just to point out that I am trying to access the string value and trying to index from 1 to end and not the cell, even though it returns multiple values, I am actually concatenating the return strings. – DEEPMALA Feb 07 '14 at 08:40
  • @DEEPMALA: See my edits; perhaps they help, but given the nature of your errors, I suspect the problem is elsewhere... – Rody Oldenhuis Feb 07 '14 at 11:51