Clear Filters
Clear Filters

Looking for data structure ideas for App Designer GUI used to set simulation inputs

3 views (last 30 days)
Hi all,
I’m looking for advice or ideas on how to manage data structures.
I have an App Designer GUI for a user to input data, which is then written to a save file (.mat). This file is loaded into an agent-based Monte Carlo simulation.
The agents used in the simulation are unlimited in number, include multiple agent types, and each instance (for each agent type) stores multiple data classes (e.g., strings, doubles, Booleans, arrays, etc.)
Currently, the data flow in the GUI is: data input field -> Map Container object -> Cell Array in the input GUI.
And, when used by the sim, the data flow is: Cell Array -> Map Container object (in a loop) -> named variable (in a function called in the loop)
For example:
% Numeric input field callback in the data input GUI
app.GreenAgent_InputsMap(Var1) = app.GreenAgent_Var1_InputField.Value;
% Write inputs map to cell array (by index) in the data input GUI, which is then saved to a .mat file
% (without the .app scope)
app.GreenAgent_InputsCells{n} = app.GreenAgent_InputsMap.keys();
app.GreenAgent_InputCells{n+1} = app.GreenAgent_InputsMap.values();
% In the simulation, the .mat file is opened (in a loop)
GreenAgent_InputsMap = containers.Map(GreenAgent_InputCells{n}, GreenAgent_InputCells{n+1});
GreenAgent_Var1 = GreenAgent_InputsMap(Var1);
I use the Map Containers to: 1) use names (like a dictionary) rather than index position in a cell array; and 2) store multiple data types. I use the Cell Arrays to unpack data for each agent type iteratively.
There are too many variables in each agent type to outright name the variables individually, especially considering an unlimited number of each agent type can be created by the user. As is, I’m effectively creating 3 copies of each variable:
  1. The App Designer input field (the input fields switch whenever a different agent of the same type is selected for editing)
  2. The Map Container key/value pair (the Map Container is overwritten whenever a different agent of the same type is selected for editing)
  3. The Cell Array where the data is ultimately stored and used by the sim (to support looping over an unlimited number of agents)
I’m wondering if there is a more elegant solution. For instance, it would be nice if each variable for an agent (i.e., in the Map Container) supported highlighting in the MATLAB editor to quickly find all the instances where that variable ('Var1' in the example code) is called and/or overwritten. I've thought about using structure arrays to take the place of the Map Containers and Cell Arrays, but that wouldn't solve the highlighting limitation, and I'm not sure if that is the best approach either.
Thanks for reading to this point!
  5 Comments
Brandon S
Brandon S on 27 May 2024
Yeah, I was leaning toward structure arrays because they would replace the Map Containers and the Cell Arrays with a single variable type, which would be cleaner. I don't remember if structure arrays support tab autocomplete because of the dot notation, but that would be helpful.
I'm not actually naming variables dynamically. I should have provided clearer example code. These variables are used in functions that are called from a loop. In the loop, the cell array is unpacked, contents written to a map object, and the map object is passed into a function. In the function, it's really like:
AgentAge = GreenAgent(Age) % GreenAgent is the map passed into the function
Thanks. I'll switch to structure arrays

Sign in to comment.

Answers (0)

Categories

Find more on Variables in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!