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

11 visualizzazioni (ultimi 30 giorni)
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

Risposte (1)

Oguz Kaan Hancioglu
Oguz Kaan Hancioglu il 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 Commenti
massimo Petrarca
massimo Petrarca il 4 Apr 2023
Modificato: David il 4 Apr 2023
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
Oguz Kaan Hancioglu
Oguz Kaan Hancioglu il 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

Accedi per commentare.

Categorie

Scopri di più su Develop uifigure-Based Apps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by