开发者

Why can't you use a object property as an index in a for loop? (MATLAB)

Below is an example that doesn't work in Matlab because obj.yo is used as the for loop's index. You can just convert this to the equival开发者_Python百科ent while loop and it works fine, so why won't Matlab let this code run?

classdef iter_test
    properties
        yo = 1;
    end
    methods
        function obj = iter_test
        end
        function run(obj)
            for obj.yo = 1:10
                disp('yo');
            end
        end
    end
end


Foreword: You shouldn't expect too much from Matlab's oop capabilities. Even though things have gotten better with matlab > 2008a, compared to a real programming language, oop support in Matlab is very poor. From my experience, Mathworks is trying to protect the user as much as possible from doing mistakes. This sometimes also means that they are restricting the possibilities. Looking at your example I believe that exactly the same is happening.

Possible Answer: Since Matlab doesn't have any explicit typing (variables / parameters are getting typed on the fly), your code might run into problems. Imagine:

$ a = iter_test()
% a.yo is set to 1

% let's overwrite 'yo'
$ a.yo = struct('somefield', [], 'second_field', []);
% a.yo is now a struct

The following code will therefore fail:

$ for a.yo
     disp('hey');
  end

I bet that if matlab would support typing of parameters / variables, your code would work just fine. However, since you can assign a completely different data type to a parameter / variable after initialization, the compiler doesn't allow you to do what you want to do because you might run into trouble.


From help

"properties are like fields of a struct object."

Hence, you can use a property to read/write to it. But not use it as variable like you are trying to do. When you write

   for obj.yo = 1:10
                disp('yo');
    end

then obj.yo is being used as a variable, not a field name.

compare to actual struct usage to make it more clear:

EDU>> s = struct('id',10)
for s.id=1:10
    disp('hi')
end

s = 

    id: 10

??? for s.id=1:10
         |
Error: Unexpected MATLAB operator.

However, one can 'set' the struct field to new value

EDU>> s.id=4
s = 
    id: 4

compare the above error to what you got:

??? Error using ==> iter_test
Error: File: iter_test.m Line: 9 Column: 20
Unexpected MATLAB operator.

Therefore, I do not think what you are trying to do is possible.


The error is

??? Error: File: iter_test.m Line: 9 Column: 20
Unexpected MATLAB operator.

Means that the MATLAB parser doesn't understand it. I'll leave it to you to decide whether it's a bug or deliberate. Raise it with TMW Technical Support.

EDIT: This also occurs for all other kinds of subscripting:

The following all fail to parse:

a = [0 1];
for a(1) = 1:10, end

a = {0 1};
for a{1} = 1:10, end

a = struct('a', 0, 'b', 0);
for a.a = 1:10, end

It's an issue with the MATLAB parser. Raise it with Mathworks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜