Difference between empty Matlab struct S and all elements S(:)

后端 未结 3 1808
死守一世寂寞
死守一世寂寞 2020-12-17 00:53

My question is: What is the difference between S and S(:) if S is an empty struct.

I believe that there is a difference becaus

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-17 01:21

    Actually the difference between S and S(:) applies to structs in general, not only to empty structs.

    One reason why this might be the case, is because this allows you to choose whether you want to access the struct or its contents.

    A practical example would be the assignment of [] in order to remove something:

    S = struct();
    T = struct();
    
    S(:) = []; % An empty struct with all fields that S used to have
    T = []; % Simply an empty matrix
    

    S is now an empty struct, but would still contain all fields that it had before.

    T on the other hand, has now simply become the empty matrix [].

    Both actions do what you would expect, and this would not be possible without the distinction between a struct and all its elements.

提交回复
热议问题