push botton

조회 수: 1 (최근 30일)
Muhammad Arsalan
Muhammad Arsalan 2011년 3월 25일
Hi,
I am writing a script and at some point i need to repeat the execution of a part of script. So i want to add a pus button in the process and the goal is when the user click the push button, that particular code should execute and do some calculations.
So wanna do something like following:
pb1 = uicontrol(figure1,'Style','pushbutton','String','Update',...
'Position',[40, 80, 120, 40]);
% Everytime user press "Update"
load('someMatFile','variable1');
plot(variable1);
so on and so forth
I am not using any function in the script so when i try to use callback function for push button, i get some error messages.
Thanks for your help in advance.
\\Arsalan

채택된 답변

Walter Roberson
Walter Roberson 2011년 3월 25일
Matt's answers are the preferred ways, but he happened to overlook a possibility to do what you showed:
set(pb1, 'Callback', ...
['plot(subsref(load(''SomeMatFile'',''variable1''), ', ...
'struct(''type'', ''.'', ''subs'', ''variable1''))' ] );
That is, the Callback can be set to a string that will be executed in the "base workspace". The command indicated by the string loads variable1 from the file, and returns a structure array that has variable1 as a field name; the field is then extracted by the subsref call, and the data that results is then plotted by plot().
You might even be able to use a string that has multiple commands separated with ';', but I have not experimented with that myself. I avoid using this kind of callback construction, doing so only when the alternatives are worse.
  댓글 수: 1
Muhammad Arsalan
Muhammad Arsalan 2011년 3월 26일
Thanks a lot Walter. I was exactly looking for the option you have showed above.

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

추가 답변 (1개)

Matt Fig
Matt Fig 2011년 3월 25일
You will either need to put the function in a separate M-file, or convert the script itself to a function so that the callback function is a subfunction.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by