按照Benoit_11在Use a slider in MATLAB GUI中给出的提示,我开始调整他的代码以适应我的情况。
我注意到当向量SliderValue*(1:0.1:20).^2
被修改为SliderValue*(1:dt:20).^2
, withdt = 0.1
时,绘图不显示任何内容。这是必需的,因为我想使用由变量定义的表达式。
第二个问题:如何手动定义轴范围?
我的代码:
%function GUI_slider
% GUI Controls
dt = 0.1;
t = 0:0.1:100;
handles.figure = figure('Position', [100 100 1000 500], 'Units', 'Pixels');
handles.axes1 = axes('Units', 'Pixels', 'Position', [60, 120, 900, 300]);
handles.Slider1 = uicontrol('Style', 'slider', 'Position', [60 40 400 25], ...
'Min', min(t), 'Max', max(t), 'SliderStep', [.01 .01], ...
'Callback', @SliderCallback);
handles.Edit1 = uicontrol('Style', 'Edit', 'Position', [150 453 100 20], ...
'String', 'Click on slider');
handles.Text1 = uicontrol('Style', 'Text', 'Position', [70 450 70 20], ...
'String', 'Slider Value:');
handles.xrange = 1:dt:20; %// Use to generate dummy data to plot
guidata(handles.figure, handles); %// Update the handles structure
function SliderCallback(~,~) %// This is the slider callback, executed when you release it or press the arrows at each extremity.
handles = guidata(gcf);
SliderValue = get(handles.Slider1, 'Value');
set(handles.Edit1, 'String', num2str(SliderValue));
plot(handles.xrange, SliderValue*(1:0.1:20).^2, 'Parent', handles.axes1);
end
%end
我应该纠正什么让它运行?