How to make a structure to be input of a function and then its updated version to be output of the function?

36 次查看(过去 30 天)
I want to make a structure to be input of a function and then do some analysis and add some fields to the structure and return the updated version of structure as the ouput of the function. I tried to use:
struct=function updatestruct(struct,...)
end
and it did not update the sruct and produced a new strucutre at the output loosing the previously stored data in the struct.

采纳的回答

Sindar
Sindar 2020-7-4
function new_struct = update_struct(old_struct,add_field, add_val)
new_struct = old_struct;
new_struct.(add_field) = add_val;
end
Notes:
  • "function" comes before output
  • don't use struct as a variable name
  8 个评论
Sindar
Sindar 2020-7-4
编辑:Sindar 2020-7-4
The nested struct aspect is key. Here's a version which can deal with either:
function new_struct = update_struct(new_struct,add_field, add_val,sub_struct)
% if no substruct is given, create field under the top struct
if nargin<4 || isempty(sub_struct)
new_struct.(add_field) = add_val;
% otherwise, create the field under the given substructure
else
new_struct.(sub_struct).(add_field) = add_val;
end
end
>> mystruct = struct('a',5,'b',struct('b1',2))
mystruct =
a: 5
b: [1×1 struct]
>> mystruct = update_struct(mystruct,'c',7)
mystruct =
a: 5
b: [1×1 struct]
c: 7
>> mystruct = update_struct(mystruct,'b2',7,'b')
mystruct =
a: 5
b: [1×1 struct]
c: 7
>> mystruct.b
ans =
b1: 2
b2: 7

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2020-7-4
s = struct();
for K = 1 : 10
s = updatestruct(s);
end
for K = 1 : 10
s = updatestruct2(s);
end
disp(s)
function struct = updatestruct(struct)
fn = char('a' + randi([0 25], 1, 5));
struct.(fn) = randi([-99 99]);
fprintf('new field: %s\n', fn);
end
function struct = updatestruct2(struct)
names = fieldnames(struct);
fn = names{randi(length(names))};
struct.(fn)(end+1) = randi([-99 99]);
fprintf('updated field: %s\n', fn);
end
Example run:
>> demo
new field: bresh
new field: iznax
new field: ypcuj
new field: amzax
new field: yeqbz
new field: jtrij
new field: fibsk
new field: ocrsf
new field: mlrtr
new field: lqgqa
updated field: amzax
updated field: mlrtr
updated field: ocrsf
updated field: yeqbz
updated field: ocrsf
updated field: ocrsf
updated field: bresh
updated field: jtrij
updated field: fibsk
updated field: fibsk
bresh: [89 86]
iznax: -11
ypcuj: -47
amzax: [-98 -37]
yeqbz: [-64 -3]
jtrij: [26 4]
fibsk: [9 -95 -10]
ocrsf: [-85 -48 -11 -93]
mlrtr: [52 -52]
lqgqa: 57
You can see clearly that contrary to your claims, previous fields and values do not get removed when you update a struct.
"For the first solution I need to change the name of structure which I hesitate to do so."
Using "struct" as a variable name is bad practice. Just select one copy of the variable name and type in a new name and MATLAB will offer to rename it to something else if you press shift-return . It is so easy that there is no good reason not to do it. If the problem is that you have existing code that has already gone through code review that uses "struct" as a variable name, then the reviewers should never have permitted you to use that as a variable name.
  2 个评论
Walter Roberson
Walter Roberson 2020-7-6
So use some other variable name only in the function header, and immediately assign it to the existing variable name
function variable_you_are_using_now = update_struct(old_struct, whatever_parameters)
variable_you_are_using_now = old_struct;
... your existing code that updates variable_you_are_using_now
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by