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

前端 未结 3 460
夕颜
夕颜 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 10:00

    I'am not sure to totally understand your question, but if you want to update a parameter in a structure, you have to pass the structure to update as argument of your function.

    Moreover, if prop is the parameter, you should use an dynamic allocation using a string in your function :

    function [ s_struct ] = set_s( s_struct, number, prop, value )
        s_struct(number).(prop) = value;
    end
    

    Using it this way :

    s_vec = set_s(s_vec, 2, 'Hello', 5);
    

    It will update the second value to the parameter 'Hello' to 5.

提交回复
热议问题