Making a nested function in within a script for a call back function

조회 수: 18 (최근 30일)
Amanda
Amanda 2022년 11월 11일
댓글: Mohsin Zubair 2022년 11월 14일
I am learning how to program a GUI programatically. I have a script called plotburst that i need to write a nested function called randomize within. I want to set randomizr as the callback function for my button. I also need to set the postion and apperance of a plotted data point to random values. I'm not sure on how to even start this problem if you could help that would be great.
  댓글 수: 6
Walter Roberson
Walter Roberson 2022년 11월 12일
Nested functions are permitted within scripts, as long as they are nested within a function.
try_experiment()
ans = 62
function result = try_experiment
shared_variable = 0;
for K = 1 : 5; try_nested(); end
result = shared_variable;
function try_nested
shared_variable = (shared_variable + 1) * 2;
end
end
Jan
Jan 2022년 11월 12일
@Walter Roberson: Thanks for the clarification.
@Amanda: "It is suppose to be a nested function 'randomize' within 'plotBurst'" - So start to write 'plotBurst' t first.
"I need to set the position and appearance of the plotted data point to random values with a reasonable range" - I cannoit guess, what you calle "the plotted data point".
The explanations are too abstract to suggest some specific code. So just a general hint: I think it is easier to read, if the parameters of a uicontrol are set directly. Compare:
my_third_button = uicontrol('style', 'pushbutton');
set(my_third_button, 'units', 'normalized','position', [0.65,0.05,0.3,0.15])
set(my_third_button, 'string', 'Randomize')
set(my_third_button, 'callback', @sayrandomize)
with
my_third_button = uicontrol('style', 'pushbutton', 'string', 'Randomize', ...
'units', 'normalized','position', [0.65, 0.05, 0.3, 0.15], ...
'callback', @sayrandomize);
Simpler code reduces the chance to insert typos and increases the chance to find them.

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

답변 (1개)

Mohsin Zubair
Mohsin Zubair 2022년 11월 11일
편집: Mohsin Zubair 2022년 11월 11일
Not sure about the content of your code so can't answer the later part but for 1st part about to create a button and assign a userdefined function as its call back you can do it like this:
f=uifigure;
uibutton(f,"ButtonPushedFcn",@randamize);
You can set position of button using "Position" property in above line.
But for working with GUI I woud suggest use App Designer instead.

카테고리

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