关于Matlab GUI

时间:2014-09-27 23:58:54

标签: matlab user-interface

在Matlab GUI中,我想要绘制:A * sin(x)。 A是幅度。我创建了一个轴,一个推动按钮和两个编辑文本,一个是"幅度",然后另一个是" A"该用户将输入。 在编码部分,我不知道该怎么做。这是我到目前为止所做的代码。

function pushbutton1_Callback(hObject, eventdata, handles)
plot(sin(0:.1:10))

function input_ampli_Callback(hObject, eventdata, handles)

function input_ampli_CreateFcn(hObject, eventdata, handles)

input = str2num(get(hObject,'String')); 

if (isempty(input))
    set(hObject,'String','0')
end

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end 

1 个答案:

答案 0 :(得分:0)

你需要告诉Matlab在哪里绘制数据,在你的情况下,它在轴上。

您展示的input_ampli_Callback和input_ampli_CreateFcn不太可能用于您的特定目的。您基本上只需要使用以下内容从用户获得振幅:

A = str2num(get(handles.input_ampli,'String'));

然后绘制函数。所以一切都可以在按钮回调中发生。因此,您的代码如下所示:

function pushbutton1_Callback(hObject, eventdata, handles)

A = str2num(get(handles.input_ampli,'String'));

axes(handles.axes1) % This is the handles of the axes object. The name might be different. This is used to tell the GUI to make this axes the current axes, so stuff will be displayed in it.

plot(A*sin(0:.1:10)); Plot your function!

当然,您可以在GUI中添加其他文本框,让用户选择要绘制的范围,但原理是相同的。希望有助于您入门!

编辑:以下是一个简单的程序化GUI的代码,您可以自定义和使用它来查看它的工作原理。它不使用指南,但原则是相同的。我使用全局变量轻松地在不同函数(回调)之间共享数据。希望也有帮助。

function PlotSin(~)

global ha hPlotSin htext hAmp
%  Create and then hide the GUI as it is being constructed.
f = figure('Visible','off','Position',[360,500,450,285]);

ha = axes('Units','Pixels','Position',[50,60,200,185]);

% Create pushbutton
hPlotSin = uicontrol('Style','pushbutton','String','Plot',...
    'Position',[315,220,70,25],...
    'Callback',{@Plotbutton_Callback});
% Create text box
htext = uicontrol('Style','text','String','Enter amplitude',...
    'Position',[325,90,60,30]);

% Create edit box to let the user enter an amplitude 
hAmp = uicontrol('Style','Edit','String','',...
    'Position',[325,50,60,30]);

% Assign the GUI a name to appear in the window title.
set(f,'Name','Simple GUI')
% Move the GUI to the center of the screen.
movegui(f,'center')
% Make the GUI visible.
set(f,'Visible','on');


end

% This is the pushbutton callback.
function Plotbutton_Callback(source,eventdata)
global ha hPlotSin htext hAmp

A = str2num(get(hAmp,'String')); % Get the amplitude

x = 0:0.1:10; % Define x...
axes(ha) % Make the axes the current axes so the function is plot in it.

plot(x,A*sin(x)) % Plot the data

end