필터 지우기
필터 지우기

(App Designer) Dropdown menu detect variables in workspace.

조회 수: 13 (최근 30일)
Saiful Adham Shukor
Saiful Adham Shukor 2018년 4월 23일
댓글: Ameer Hamza Khan 2019년 2월 22일
I'm try to familiar myself with apps designer. How to create a dropdown menu with the list of variables in workspace. so, user only choose their selected variable/dataset.
  댓글 수: 1
Stephen23
Stephen23 2018년 4월 24일
편집: Stephen23 2018년 4월 24일

Note that your code will be simpler and more robust when data is passed between functions, e.g. as input/output arguments, nested functions, or using guidata. Magically accessing variable in other workspace is always slow, complex, and liable to bugs. Read this to know more:

https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval

댓글을 달려면 로그인하십시오.

채택된 답변

Ameer Hamza
Ameer Hamza 2018년 4월 24일
Yes, it is possible. Here is one way. Add a drop-down menu to the app. In the COMPONENT BROWSER right-click app.UIFigure and add a startupFcn callback for figure object. You can define the newly created function like this
function startupFcn(app)
vars = evalin('base', 'whos');
cell_array = cell(size(vars));
for i=1:length(vars)
cell_array{i} = vars(i).name;
end
app.DropDown.Items = cell_array;
end
where DropDown is the name of your drop-down menu. Now start the app, the drop-down menu will enlist all the variables from the workspace.
  댓글 수: 2
Saiful Adham Shukor
Saiful Adham Shukor 2018년 4월 24일
Thank You Ameer Hamza. I need this one. tqsm again
Ameer Hamza
Ameer Hamza 2018년 4월 24일
You are welcome.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Abdelmalek Benaimeur
Abdelmalek Benaimeur 2019년 2월 21일
thank you so much Ameer Hamza please I have a question
i tried the method you gave us and it worked, but the problem i found is when i open the app i've made, and when i add a new matrix in the workspace it doesn't show up automatically in the dropdown menu until i close the app and re-open it again
is there a way to solve this problem please?
  댓글 수: 1
Ameer Hamza Khan
Ameer Hamza Khan 2019년 2월 22일
One way is to use timer callback. You can use a timer to automatically update the menu at regular (say after every 1 second). Instead of writing my code in startupFcn() you can create a new function inside your app, paste this code in it and assign that function to a timer as callback.

댓글을 달려면 로그인하십시오.


Abdelmalek Benaimeur
Abdelmalek Benaimeur 2019년 2월 22일
please could you show me how, beacause i'm new to appdesigner and i didnt' figure out how to do it
  댓글 수: 1
Ameer Hamza Khan
Ameer Hamza Khan 2019년 2월 22일
The attached file contain an example code. In summary i created a property and a callback function.
properties (Access = private)
menu_timer % Description
end
methods (Access = private)
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.DropDown.Items = cell_array;
end
end
and added following figure startup and close functions:
function startupFcn(app)
app.menu_timer = timer('ExecutionMode', 'fixedRate', ...
'Period', 1, ...
'TimerFcn', @app.update_menu);
start(app.menu_timer);
end
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
stop(app.menu_timer);
delete(app.menu_timer);
delete(app)
end

댓글을 달려면 로그인하십시오.


Abdelmalek Benaimeur
Abdelmalek Benaimeur 2019년 2월 22일
you are the Best, Ameer, thank you so much

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by