How to update the Struct value in the workspace from the MATLAB GUI app designer?

21 views (last 30 days)
I am designing the GUI using the matlab app builder, I want to change(assign) the values of struct which is already present in the workspace using the GUI , but I have used the assignin function for 'variables' its works well but for the 'struct' I dont know how to change.
I want to change the car.year value from the GUI.

Accepted Answer

dpb
dpb on 14 Aug 2021
A struct variable is a variable, just as any other...
function testit(s)
v=inputname(1);
s.year=s.year+10;
assignin('base',v,s);
end
Illustration --
>> car=struct('year',2020,'loan',25000,'counts',10)
car =
struct with fields:
year: 2020
loan: 25000
counts: 10
>> testit(car)
>> car
car =
struct with fields:
year: 2030
loan: 25000
counts: 10
>>
  3 Comments
dpb
dpb on 15 Aug 2021
You adapt the idea of the function to your usage -- if the struct in question is fixed and immutable, then you can simply encode the variable name as text.
If it can be variable, then you'll need to be able to pass the name to the function and keep it as part of the app data structures.

Sign in to comment.

More Answers (0)

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!