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

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 comentarios

"I'm not sure on how to even start" - This is hard to answer. Maybe install Matlab? Or buy a computer? Did you read Matöab's Onramp tutorial already?
Do you create a figure or uifigure? Do you write it by functions or object oriented?
Do not start with nested subfunctions having a specific name but just open a figure, create a button and let the callback print "hello world". Then expand the code. Post the code to aks specific questions.
Amanda
Amanda el 11 de Nov. de 2022
Editada: Jan el 11 de Nov. de 2022
f= figure;
ax= axes('Parent',f,'Position',[0.05, 0.25, 0.9, 0.7]);
xlim = [0 1];
ylim = [0 1];
x = 0.25;
y = 0.55;
point_plot_handle = plot(x,y,'d','MarkerFaceColor','m','MarkerSize',75);
hold on;
my_button = uicontrol('style','pushbutton');
set(my_button, 'units', 'normalized', 'position', [0.05,0.05,0.3,0.15])
set(my_button, 'string','Add Point')
my_second_button = uicontrol('style','pushbutton');
set(my_second_button, 'units', 'normalized', 'position', [0.35,0.05,0.3,0.15])
set(my_second_button, 'string', 'Clear')
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)
This is what I have so far. I am aware that app designer would be easier but it for an assingment and this is how my professor has asked us to do it. I would just like some help not saracasm, thanks in advance
Okay, now it is getting clearer.
Do you really want a "nested function"? This is not possible inside a script. Convert the script to function, which is a good strategy to write reliable code at all.
Nested functions share data with the surrounding function. This can be useful or confusing. If nesting is not required, prefer a standard function and share the data e.g. by guidata():
function sayrandomize(ButtonH, EventData)
end
What does this exactly mean: "I also need to set the postion and apperance of a plotted data point to random values"?
It is suppose to be a nested function 'randomize' within 'plotBurst'. Then I need to Set randomize as the callback function for the button labeled “Randomize”. In the function, I need to set the position and appearance of the plotted data point to random values with a reasonable range.So I need to take the plotted point and make it randomize when I press the button.
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
@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.

Iniciar sesión para comentar.

Respuestas (1)

Mohsin Zubair
Mohsin Zubair el 11 de Nov. de 2022
Editada: Mohsin Zubair el 11 de Nov. de 2022
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.

2 comentarios

I have posted my code above if that helps
@Amanda @Jan has explained it much better already

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 11 de Nov. de 2022

Comentada:

el 14 de Nov. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by