how to list variable name and values in the WS into a dropdown menu

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)

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

Thank you.
by using this suggestion " evalin('base','baseArray')"
now it works.
so now it is :
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
function SavetoWorkspaceButtonPushed(app, event)
%value = app.PLUTODropDown.Value;
dasalvare=evalin('base',app.PLUTODropDown.Value)
app.dataname;
assignin('base',app.dataname,dasalvare)
end
Hello,
I was wondering if you can help also understanding how read the name and the values of the variable that the MAtlab app designer uses but that are not shown in the workspace.
What I mean is:
I load a file and obtain two variable from the graphic interface of Matlab.
Now the two variable are not by default shown in the WS.
So I used to save them to the WS and then call them back if needed.
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?
thank you
Max
"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:
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.
Hi,
what I need is a dropdown menu that shows me and allows me to select the different variables I am using and creating.
So far, I was able to have a dropdown menu capable to read and select the variables in the base WS .
For example I open a file and create two array within app. designer. But I am not able to see them in the dropdown menu and select their values to make further operation. So I save them in the base WS and then I can read and select them from the dropdown menu thanks to the script I shown on top.
how can I have my dropdown menu showing the vector I produce and allowing me to select them for further operation?
thank you again
Max
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
Hi,
thanks this is useful.
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.
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.
Is this possible ? or may be I am not getting your suggestion to the bottom...
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
Dear all,
thank you for your answer.
I apologise but I still do not know how to do it. In my code I used public properties.
Below I post my code so far. With this code I can in the dropdown see and load the wariable in the base WS change their name and save them again in the WS.
But I do not know how to tell the dropdown menu how to show the arays created in the App designer and how to shoe the new arrays that I can created during the app is running.
I am probably missing some basic knowledge.
Is there any example ?
thanks
Max
classdef CaricaVariabili_v1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
PLUTODropDownLabel matlab.ui.control.Label
PLUTODropDown matlab.ui.control.DropDown
LoadDataButton matlab.ui.control.Button
NameDataEditFieldLabel matlab.ui.control.Label
NameDataEditField matlab.ui.control.EditField
SavetoWorkspaceButton matlab.ui.control.Button
FFTButton matlab.ui.control.Button
PIPPODropDownLabel matlab.ui.control.Label
PIPPODropDown matlab.ui.control.DropDown
DIVIONEButton matlab.ui.control.Button
DeleteVarButton matlab.ui.control.Button
end
properties (Access = public)
aggiorna=1 %inizializzo il valore a 1
Property % Description
menu_timer % Description
dataname
x
y
segnale1
segnale2
frequenza1
frequenza2
fft_Campo1
campo1
campo2
asse_t1
risultato
end
methods (Access = public)
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;
app.PIPPODropDown.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 results = aggiornaGrafico(app)
app.dataname
end
function [fft_segnale,asse_t] = FFT(app, segnaleingresso, frequenze)
f=frequenze;
N=length(f);
df=(f(end)-f(1))/length(f) % in GHz
RangeF=(f(end)-f(1))
DT=1/RangeF % in ns
rangeT=DT*N
%%
TT1=-rangeT/2:DT:(rangeT/2)-DT;
%TT1=0:DT:(rangeT)-DT;
TT1=TT1';
asse_t=TT1;
fft_segnale = fftshift(fft(fftshift(segnaleingresso)));
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.menu_timer = timer('ExecutionMode', 'fixedRate', ...
'Period', 1, ...
'TimerFcn', @app.update_menu);
start(app.menu_timer);
end
% Value changing function: NameDataEditField
function NameDataEditFieldValueChanging(app, event)
%changingValue = event.Value;
app.dataname = event.Value;
%app.Centrosl1 = event.Value;
if app.aggiorna
aggiornaGrafico(app)
end
end
% Button pushed function: SavetoWorkspaceButton
function SavetoWorkspaceButtonPushed(app, event)
%value = app.PLUTODropDown.Value;
dasalvare=evalin('base',app.PLUTODropDown.Value) %leggo i valori della stringa che seleziono con il dropdown menu e che sono le WS e li associo alla variabile "dasalvare"
app.dataname;
assignin('base',app.dataname,dasalvare) % app.dataname e' una stringa gia' quindi non devo mettere gli apici
end
% Button pushed function: LoadDataButton
function LoadDataButtonPushed(app, event)
[filename, pathname] = uigetfile({'*.csv'},'File Selector');
if ~ischar(filename)
return; % User aborted the file selection
end
file = fullfile(pathname, filename)
fid = fopen(file, 'r')
B=textscan(fid, ' %f %f %f ' , 'Delimiter', ',,' ,'HeaderLines',1)
C=cell2mat(B);
app.y=C(:,2); %campo
app.x=C(:,1); %frequenze segnale 1
%app.tempo=zeros(1,length(C(:,3)))';
%tempo=app.tempo';
%app.campo1=app.y;
assignin('base','field1',app.y)
assignin('base','t1',app.x)
end
% Value changed function: PLUTODropDown
function PLUTODropDownValueChanged(app, event)
value = app.PLUTODropDown.Value;
end
% Button pushed function: FFTButton
function FFTButtonPushed(app, event)
Ydatrasformare=evalin('base',app.PLUTODropDown.Value)
Xdatrasformare=evalin('base',app.PLUTODropDown.Value)
[app.fft_Campo1,app.asse_t1] = FFT(app, Ydatrasformare , Xdatrasformare);
assignin('base','ytrasf',app.fft_Campo1)
assignin('base','xtrasf',app.asse_t1)
end
% Button pushed function: DIVIONEButton
function DIVIONEButtonPushed(app, event)
Y=evalin('base',app.PLUTODropDown.Value)
X=evalin('base',app.PIPPODropDown.Value)
app.risultato=Y./X;
assignin('base','y_div_x',app.risultato)
end
% Button pushed function: DeleteVarButton
function DeleteVarButtonPushed(app, event)
evalin('base','clear all')
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 971 475];
app.UIFigure.Name = 'MATLAB App';
% Create PLUTODropDownLabel
app.PLUTODropDownLabel = uilabel(app.UIFigure);
app.PLUTODropDownLabel.HorizontalAlignment = 'right';
app.PLUTODropDownLabel.Position = [76 355 45 22];
app.PLUTODropDownLabel.Text = 'PLUTO';
% Create PLUTODropDown
app.PLUTODropDown = uidropdown(app.UIFigure);
app.PLUTODropDown.ValueChangedFcn = createCallbackFcn(app, @PLUTODropDownValueChanged, true);
app.PLUTODropDown.Position = [136 355 100 22];
% Create LoadDataButton
app.LoadDataButton = uibutton(app.UIFigure, 'push');
app.LoadDataButton.ButtonPushedFcn = createCallbackFcn(app, @LoadDataButtonPushed, true);
app.LoadDataButton.Position = [49 431 100 22];
app.LoadDataButton.Text = 'LoadData';
% Create NameDataEditFieldLabel
app.NameDataEditFieldLabel = uilabel(app.UIFigure);
app.NameDataEditFieldLabel.HorizontalAlignment = 'right';
app.NameDataEditFieldLabel.Position = [202 431 62 22];
app.NameDataEditFieldLabel.Text = 'NameData';
% Create NameDataEditField
app.NameDataEditField = uieditfield(app.UIFigure, 'text');
app.NameDataEditField.ValueChangingFcn = createCallbackFcn(app, @NameDataEditFieldValueChanging, true);
app.NameDataEditField.Position = [279 431 100 22];
% Create SavetoWorkspaceButton
app.SavetoWorkspaceButton = uibutton(app.UIFigure, 'push');
app.SavetoWorkspaceButton.ButtonPushedFcn = createCallbackFcn(app, @SavetoWorkspaceButtonPushed, true);
app.SavetoWorkspaceButton.Position = [425 431 113 22];
app.SavetoWorkspaceButton.Text = 'SavetoWorkspace';
% Create FFTButton
app.FFTButton = uibutton(app.UIFigure, 'push');
app.FFTButton.ButtonPushedFcn = createCallbackFcn(app, @FFTButtonPushed, true);
app.FFTButton.Position = [136 233 100 22];
app.FFTButton.Text = 'FFT';
% Create PIPPODropDownLabel
app.PIPPODropDownLabel = uilabel(app.UIFigure);
app.PIPPODropDownLabel.HorizontalAlignment = 'right';
app.PIPPODropDownLabel.Position = [80 168 41 22];
app.PIPPODropDownLabel.Text = 'PIPPO';
% Create PIPPODropDown
app.PIPPODropDown = uidropdown(app.UIFigure);
app.PIPPODropDown.Position = [136 168 100 22];
% Create DIVIONEButton
app.DIVIONEButton = uibutton(app.UIFigure, 'push');
app.DIVIONEButton.ButtonPushedFcn = createCallbackFcn(app, @DIVIONEButtonPushed, true);
app.DIVIONEButton.Position = [59 47 100 22];
app.DIVIONEButton.Text = 'DIVIONE';
% Create DeleteVarButton
app.DeleteVarButton = uibutton(app.UIFigure, 'push');
app.DeleteVarButton.ButtonPushedFcn = createCallbackFcn(app, @DeleteVarButtonPushed, true);
app.DeleteVarButton.Position = [655 431 100 22];
app.DeleteVarButton.Text = 'Delete Var';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = CaricaVariabili_v1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
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

Sign in to comment.

Categories

Find more on Develop Apps Programmatically in Help Center and File Exchange

Asked:

on 30 Mar 2023

Community Treasure Hunt

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

Start Hunting!