How to change the Value of a struct with a function in Matlab?

前端 未结 3 430
夕颜
夕颜 2021-01-28 09:37
s= struct(\'Hello\',0,\'World\',0);
for i = 1: 5
     s_vec(i) = s;
end

I have definied a struct in Matlab within a script. Now i want to implement a f

3条回答
  •  爱一瞬间的悲伤
    2021-01-28 09:56

    If you want changing data outside your function (aka side effects) use classes instead of structures. Class must be a handle.

    classdef MutableStruct < handle
        properties
            field1;
            field2;
        end
        methods
            function this = MutableStruct(val1, val2)
                this.field1 = val1;
                this.field2 = val2;
            end
        end
    end
    

    There is more information about correct initialization of object arrays: MATLAB: Construct Object Arrays

提交回复
热议问题