how to list variable name and values in the WS into a dropdown menu
Show older comments
Dear all,
I would like to have a dropdown menu that allows me to find and select the variables that I have in my workspace. So that I can use the associated data for other operation.
For example in my workspace I have a variable A that is an array. I would like that in the dropdown menu I see A and when I select it I can use its values (the array)
So far I was able to list in the dropdown meny the variable name but those are just string and do not have the values.
How can I do?
With this code I found in the forum I can make the first part. But then the dropbown menu shows me the names of the varible only. I do not know how to associate to them te values and therefore in the last part in the "assignin" I just have a new variable called app.dataname = value but value is just the name of the variable from the dropdown and its value.
Please any help?
Thank you
Max
function update_menu(app, ~, ~)
vars = evalin('base', 'whos');
cell_array = cell(size(vars));
for i=1:length(vars)
cell_array{i} = vars(i).name;
end
app.PLUTODropDown.Items = cell_array;
end
function results = UIFigureCloseRequest(app, event)
% Close request function: UIFigure
stop(app.menu_timer);
delete(app.menu_timer);
delete(app)
end
function SavetoWorkspaceButtonPushed(app, event)
value = app.PLUTODropDown.Value;
app.dataname;
assignin('base',app.dataname,value) % app.dataname e' una stringa gia' quindi non devo mettere gli apici
end
Answers (1)
Oguz Kaan Hancioglu
on 30 Mar 2023
You can read any value from workspace using evalin command.
appArray = evalin('base','baseArray')
Unfortunately, in the app designer mosts of the menu are cell. After you read your array you need to create cell array in order to use in dropdown value. You can compare cell values or convert to double in order to use in your app.
9 Comments
massimo Petrarca
on 30 Mar 2023
Moved: Stephen23
on 3 Apr 2023
massimo Petrarca
on 3 Apr 2023
Stephen23
on 3 Apr 2023
"the name and the values of the variable that the MAtlab app designer uses but that are not shown in the workspace."
Which workspace do you mean? The workspaces within the app's callback functions are where you should be processing your data, and all of the app's properties and variables are easily available there:
https://www.mathworks.com/help/matlab/creating_guis/share-data-across-callbacks-in-app-designer.html
Making everything magically appear in the base workspace just makes everything more complex.
"Now is it possible to know and so to list the name of the variable and their values in the dropdown menu without saving them first on the WS?"
Yes: simply process the data inside the app, just like GUIs are designed for.
massimo Petrarca
on 3 Apr 2023
Oguz Kaan Hancioglu
on 3 Apr 2023
You can use struct array to keep you data in your app designer. For example;
S(1).number = 1;
S(1).name = 'First Array';
S(1).data = [......];
S(2).number = 2;
S(2).name = 'First Array';
S(2).data = [......]
When you create new array, you just need to add new element to the struct. After that, you can show this struct (maybe jsut struct's name) in the dropdown menu.
You can use or create another sturcture just like this.
Best
massimo Petrarca
on 3 Apr 2023
Oguz Kaan Hancioglu
on 3 Apr 2023
Hi,
The problem is that in this case I need every time I create a new array to add an item to the strycture array by writing the code. I do not know if this can be done dynamically.
Yes, you can do it dynamically.
In the code I posted ubove, every time there is a e new array for example it is seen and the name is added to the drpdown. In this case the only problem for me is that I am so far able to see only the array in base WS and not those that I created in the app design.
Each function, gui, simulink have their individual scope. WS is the main scope. App designer is a event based system. Therefore when you implement your code within calbacks of the scope of the array that you created within the callback. In order to access anywhere in the app designer you need to design in public property as @Stephen23 mentioned earlier. In short, you can save or access you array by assigning app.myarray = [...]
Best
massimo Petrarca
on 4 Apr 2023
Edited: David
on 4 Apr 2023
Oguz Kaan Hancioglu
on 4 Apr 2023
You can do it dynamically.
There is another solution. You can use assignin command to define your arrays in the base workspace. After than you can use them in the dropdown menu.
When you create a new array assign to base workspace.
assignin('base','myArrayName',myArray);
I hope it works.
Best
Categories
Find more on Develop Apps Programmatically 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!