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

조회 수: 42 (최근 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
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2020년 7월 4일
@Sindar @walter Robrson
By the way it is nested structure like:
struct.fieldA
and I am going to add
struct.fieldA.fieldAA
struct.fieldA.fieldAB
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
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2020년 7월 6일
using strucut as the name was only for asking question. I did not use it in the reall code. Thanks
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