Uicontrol to change variable and plot live

25 次查看(过去 30 天)
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
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.
aneps
aneps 2021-2-10
编辑:aneps 2021-2-10
Thanks for the suggestion. Instead of opening the file, I am now using importdata and I removed fopen. Also, now I am not importing everytime. I put it out of the loop.
Would you mind to tell me how can I configure the callback for the slider?

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2021-2-10
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 个)

类别

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