2014-09-27 75 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