import struct as property in App Designer
11 views (last 30 days)
Show older comments
I'd like to load a struct as a .mat file into my app, input values and then save changes. I'd like to import it as a property. I can do it using:
properties (Access = private)
myStruct
end
And then loading it on startup:
function startupFcn(app)
myStruct = load('DemoStruct.mat');
end
This works and I am able to edit and save the struct, but I don't get the option of autocomplete when using app.myStruct. as a way to access the fields in the struct. Is there any way to do this? The only way I can get it to work is by manually typing and this is a pain when there are many fields and indexes at play.
0 Comments
Answers (2)
Prajith Chilummula
on 10 Jan 2019
The autocompletion completion uses static analysis, so the variables won’t be completed unless defined in the workspace. During editing, the field names of a struct are not known as they are unavailable in the workspace. Therefore you cannot access the members of the struc variables. But autocomplete can be used while debugging the program. A static code analysis might be misleading also, because fields can be created dynamically also.
Stefan
on 12 Nov 2019
Edited: Stefan
on 12 Nov 2019
You could create a class instead of a dynamic struct. For all static properties you will get auto completion.
classdef myStructClass
properties
StaticProperty1
StaticProperty2
DynamicProperties
end
methods
end
end
If you still need some dynamic Properties, you could create a Struct inside the DynamicProperties or similar
properties (Access = private)
myStruct myStructClass;
end
methods
function startupFcn(app)
x = load('DemoStruct.mat')
myStruct = x.myStruct
myStruct.StaticProperty1 = 1; %Auto completion
myStruct.DynamicProperties.I_Need_This_Dynamically = 1; %No Auto completion
end
end
0 Comments
See Also
Categories
Find more on Develop Apps Using App Designer 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!