I have a problem I do not find a solution to (or maybe I just did not understand it elsewhere on the website...).
Here is my problem: I use a GUI as a uifigure and uibutton inside. When the uibutton is pressed, it calls a function that calculates a curves with input agruments and displays it in the uifigure. The idea of the interface is to press buttons to change variables value and observe the effect on the curve in the display section. So, in the same time than the display, I need to change a variable value in the main script when the button is pushed. My problem is that I cannot find a way to change this variable value at the same time the curve is calculated and displayed.
Here is the script (I simplified it but this gives an idea):
tp = 1;
gui_fig = uifigure('Position',[100 100 1300 700]);
p = uipanel(gui_fig,'Position',[10 10 1000 680]);
p.AutoResizeChildren = 'off';
Btntp_min = uibutton(gui_fig, ...
"Text","-1%", ...
"fontsize",14,"fontweight",'bold', ...
"Position",[1060 475 100 30], ...
"ButtonPushedFcn", @(src,event) Button_tp_min(p,tp));
function Button_tp_min(ax,tp)
tp = tp*0.99; % Effect of button
bx1 = subplot(2,1,1,'parent',ax);
plot(bx1,1,tp,'o')
end
In this script, the point is to reduce by 1% the value of tp each time the button is pressed. It works the first time, but the reference value keeps being that in the main script. So how is it possible to modify this script so that the new value of tp is considered in the main script ?
This is because the value of
tpyou initiate when you define the function is never updated, it is always1when the functionButton_tp_minis fired by the button.One way to have it work is to reserve a field in the application data of your figure. You can save and retrieve application data with the functions setappdata and getappdata respectively.
You first initiate the application data field when you create the figure and save the starting value of
tp. Then each time the functionButton_tp_minruns, it will:tp, thentpin the the "TP" application data field.By adding 4 lines to your code, using these application data, you now have a variable
tpwhich is dynamic. Also note that since the variabletpis retrieved directly from within theButton_tp_minfunction, it is no longer necessary to provide it as input argument of the function.Just as a side note, your
tpvalue will decrease by 1% of the previous value oftp(not by 1% of 100 as initialy). To give you an example:Will give the first 10 values of
tpas:If that is what you wanted everything is fine, otherwise if you wanted a linear decrease (like [100 99 98 97 ...]), consider changing the line
tp = tp*0.99;intotp = tp-1;