Update a value in a struct in another function
Show older comments
I have a struct which is initialized by this:
function myStruct = defult_config
myStruct.myLenth = 1;
end
myStruct.myLength = 1 is an initial value and it needs to be updated by another fuction, myUpdate:
function out = myUpdate(myStruct)
myStruct.myLength = 2;
out = [];
end
However, myUpdate doesn't update myStruct and myStruct.myLength is still shown 1.
Any way to update a value in a struct by another function?
Accepted Answer
More Answers (1)
You must return the modified myStruct from myUpdate():
myStruct.myLength = 1
myStruct = myUpdate(myStruct)
function myStruct = myUpdate(myStruct)
myStruct.myLength = 2;
end
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!