如何使用Matlab中的函数更改结构的值? [英] How to change the Value of a struct with a function in Matlab?
问题描述
s= struct('Hello',0,'World',0);
for i = 1: 5
s_vec(i) = s;
end
我已经在脚本中的 Matlab 中定义了一个结构体.现在我想实现一个改变参数值的函数.例如:
I have definied a struct in Matlab within a script. Now i want to implement a function witch change the Value of the Parameters. For example:
function s_struct = set_s (number, prop , value)
s_struct(number).prop = value;
但是函数返回一个新的结构体.它不会改变我的输入结构.我的错误在哪里?
But the function returns a new struct. It does not change my input struct. Where is my mistake?
推荐答案
我不确定是否完全理解您的问题,但是如果您想更新结构中的参数,则必须将结构作为参数传递给更新你的功能.
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.
此外,如果 prop 是参数,您应该在函数中使用字符串进行动态分配:
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
以这种方式使用它:
s_vec = set_s(s_vec, 2, 'Hello', 5);
它会将参数Hello"的第二个值更新为 5.
It will update the second value to the parameter 'Hello' to 5.
这篇关于如何使用Matlab中的函数更改结构的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!