Uicontrol to change variable and plot live
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
aneps
el 10 de Feb. de 2021
Respondida: Walter Roberson
el 10 de Feb. de 2021
Hi, I want to make a plot, change the variable and update the plot interactively. For that I am using the following code with the help of uicontrol. But it is always giving error. Could you please check the code and correct it?
Here first I call a figure, create a pushbutton. Handle h2 make a slider on the figure where I update the value for a parameter. Then I create the function. After the function the 'SliderValue' will take the value from the slider in the figure and run the function to make the plot.
What I am tyring here is, whenever I change the value of a parameter used in the function through the slider, it should update the plot automatically. Following is my code:
close all;clear all; clc;
figure()
PushButton = uicontrol(gcf, 'Style', 'push', 'String', 'Plot', ...
'Position', [300 10 30 30], ...
'CallBack', @FieldCalc, ...
'UserData', 0);
h2 = uicontrol('style','slider','min',-250,'max',-100,'val',-230, ...
'position',[20 10 260 30]);
function FieldCalc(value1,~)
Name = 'ResistanceSeries';
T1=-290;
T2=-170;
T3=value1;
T4=55;
n = 1;
filename = [Name,num2str(n),'.dat'];
[fid, message] = fopen(filename, 'rt');
MyData=load(filename);
ElNum = MyData(:,1)-1 ;
ResSer = MyData(:,:);
Funnel = ResSer(1:37, 1:2);
AccuReg = ResSer(38:71, 1:2);
PlanarReg = ResSer(72:76, 1:2);
TIMSReg = ResSer(77:116, 1:2);
ExitReg = ResSer(117:128, 1:2);
Eff_Res_Accu = sum(AccuReg(:,2));
Eff_Res_Plan = sum(PlanarReg(:,2));
Eff_Res_TIMS= sum(TIMSReg(:,2));
ElField_Accu = ((T2-T1)/Eff_Res_Accu).*AccuReg(:,2);
ElField_Plan = ((T3-T2)/Eff_Res_Plan).*PlanarReg(:,2);
ElField_TIMS = ((T4-T3)/Eff_Res_TIMS).*TIMSReg(:,2);
plot(AccuReg(:,1),ElField_Accu)
hold on
plot(PlanarReg(:,1),ElField_Plan)
hold on
plot(TIMSReg(:,1),ElField_TIMS)
hold off
%linkdata on
end
SliderValue = get(h2,'Value');
FieldCalc( SliderValue)
2 comentarios
Walter Roberson
el 10 de Feb. de 2021
[fid, message] = fopen(filename, 'rt');
Why are you opening the file? You do not do any other I/O operations on the file, including that you do not close the file and you do not test whether the open succeeded.
Why are you loading the data every time, instead of loading it once and storing it somewhere you callback can find?
If you want the plot to update automatically when you move the slider, you should configure a callback for the slider.
Respuesta aceptada
Walter Roberson
el 10 de Feb. de 2021
PushButton = uicontrol(gcf, 'Style', 'push', 'String', 'Plot', ...
'Position', [300 10 30 30], ...
'UserData', 0);
h2 = uicontrol('style','slider','min',-250,'max',-100,'val',-230, ...
'position',[20 10 260 30]);
set([PushButton, h2], 'CallBack', {@FieldCalc, h2});
function FieldCalc(~, ~, h2)
Name = 'ResistanceSeries';
T1=-290;
T2=-170;
T3 = h2.Value;
Note that setting the callback for the pushbutton was postponed until h2 had been assigned to, so that h2 would exist to be used in the callback construction.
The {FUNCTION, data} construction for callbacks is equivalent to coding
set([PushButton, h2], 'CallBack', @(hObject,event)FieldCalc(hObject,event,h2));
Your earlier try assumed that the first parameter to the callback was a specific numeric value. That is not the case. The first parameter to a callback is the handle of the object that the callback is with respect to. When triggered from the pushbutton that would be the handle of the pushbutton; when triggered from the slider that would be the handle of the slider. Because the function might receive the handle of the pushbutton there, it cannot rely on being able to access the Value property of the handle expecting to get the slider value.
Now, we could use various techniques for the callback to reliably track down the handle of the slider, but really the easiest way is just to construct the slider first and then pass its handle in as additional parameters to the function.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!