2014-09-27 73 views
-1

在Matlab GUI中,我想要绘制:A * sin(x)。 A是幅度。我创建了一个轴,一个按钮和两个编辑文本,一个是“振幅”,另一个是用户输入的“A”。 在编码部分,我不知道该怎么做。这里是我迄今为止所做的代码。关于Matlab GUI

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

你有一个复杂的作业任务,让我们为你做,我是对吗?问题的问题......这是你的教授给你的代码,你没有表现出任何努力。提示:至少删除提示。没有冒犯,如果我的假设是错误的,我很抱歉。但功课题和“请给我代码”在这里并不受欢迎。总是显示一些自己的努力。题外话:你的教授会知道这个网站,如果你注册了你的真实姓名,这可能不会有帮助;) – thewaywewalk 2014-09-28 06:42:21

+0

大声笑,谢谢你的建议,听起来不错。这不是一项家庭作业。我已经解决了它。 – 2014-09-28 14:15:45

回答

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 
+0

这是正确的!非常感谢 – 2014-09-28 01:40:46

+0

伟大,那么你不客气! – 2014-09-28 01:46:59