import struct as property in App Designer

12 visualizaciones (últimos 30 días)
ChiPhi85
ChiPhi85 el 6 de En. de 2019
Editada: Stefan el 12 de Nov. de 2019
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.

Respuestas (2)

Prajith Chilummula
Prajith Chilummula el 10 de En. de 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.
  1 comentario
ChiPhi85
ChiPhi85 el 10 de En. de 2019
Is there no way to load the struct into the workspace from the app?

Iniciar sesión para comentar.


Stefan
Stefan el 12 de Nov. de 2019
Editada: Stefan el 12 de Nov. de 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

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by