Uicontrol to change variable and plot live
显示 更早的评论
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 个评论
Walter Roberson
2021-2-10
[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.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!