How do I display a graph with a calculate function in GUI?

10 ビュー (過去 30 日間)
Bastion
Bastion 2011 年 8 月 3 日
コメント済み: VISHWAS CHAVAN 2017 年 1 月 23 日
I'm developing a GUI in MATLAB trying to display a graph of concentration with respect to time.
I have coded a function that will calculate my concentration and I have made a button known as 'Calculate' which display the outcome after I input a few variables.
Now I want to make another button called 'Display' so when I push it, it will generate a plot with concentration to time.
I can't seem to find any info on this, any help would be great.
  2 件のコメント
Arturo Moncada-Torres
Arturo Moncada-Torres 2011 年 8 月 10 日
Bastion 1 day ago
Here is what I have done:
% --- Executes on button press in Button_CALC_C.
function Button_CALC_C_Callback(hObject, eventdata, handles)
A = get(handles.input_A,'String');
B = get(handles.input_B,'String');
X = get(handles.input_X,'String');
T1 = get(handles.input_T1,'String');
T2 = get(handles.input_T2,'String');
sum1 = str2num(A) + str2num(B)*str2num(X) + str2num(T1)^2;
C1 = num2str(sum1);
set(handles.text_C1,'String',C1);
guidata(hObject, handles);
sum2 = str2num(A) + str2num(B)*str2num(X) + str2num(T2)^2;
C2 = num2str(sum2);
set(handles.text_C2,'String',C2);
guidata(hObject, handles);
% --- Executes on button press in Button_Plot_TvC.
function Button_Plot_TvC_Callback(hObject, eventdata, handles)
axes(handles.T_C_Plot);
x = T1:1:T2; % [I WONDER WHY THIS LINE DOESN'T WORK? I WANT TO PLOT THE RANGE BETWEEN T1 AND T2 WITH THE RANGE BETWEEN C1 AND C2 FROM THE CODE IN THE PREVIOUS BUTTON]
y = C1:1:C2; % [I hope someone can help]
plot (x,y);
title('Time vs Concentration');
xlabel('Time');
ylabel('Concentration');
guidata(hObject, handles);
VISHWAS CHAVAN
VISHWAS CHAVAN 2017 年 1 月 23 日
Hello, Give the handle command for every plot command like plot(handles.Tag name of plot,x,y); same for the title and other command give the handle of the plot.

サインインしてコメントする。

採用された回答

Arturo Moncada-Torres
Arturo Moncada-Torres 2011 年 8 月 10 日
I have tried the following code and it works. The problem is that x and y were not the same size. Insert the following code.
% --- Executes on button press in Button_CALC_C.
function Button_CALC_C_Callback(hObject, eventdata, handles)
% Choose default command line output for Plot_One
handles.output = hObject;
%Get user input from GUI
A = str2double(get(handles.input_A,'String'));
B = str2double(get(handles.input_B,'String'));
X = str2double(get(handles.input_X,'String'));
T1 = str2double(get(handles.input_T1,'String'));
T2 = str2double(get(handles.input_T2,'String'));
%Calculate data
sum1 = A + B * X + T1^2;
C1 = num2str(sum1);
set(handles.text_C1,'String',C1);
guidata(hObject, handles);
sum2 = A + B * X + T2^2;
C2 = num2str(sum2);
set(handles.text_C2,'String',C2);
guidata(hObject, handles);
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in Button_Plot_TvC.
function Button_Plot_TvC_Callback(hObject, eventdata, handles)
% Choose default command line output for Plot_One
handles.output = hObject;
axes(handles.T_C_Plot);
T1 = str2double(get(handles.input_T1,'String'));
T2 = str2double(get(handles.input_T2,'String'));
C1 = str2double(get(handles.text_C1,'String'));
C2 = str2double(get(handles.text_C2,'String'));
%%OPTION 1
% Create a 1D array from T1 to T2 in intervals of 1.
% x = T1:1:T2;
% Here you count the number of elements x has (type "doc numel" without
% the quotes for more information).
% noIntervals = numel(x);
% Create a 1D array from C1 to C2 in intervals of noIntervals.
% y = C1:noIntervals:C2;
%%OPTION 2
% Alternatively, you can do the following:
% Define the number of points you want to plot.
noPoints = 100;
% Create a 1D array from T1 to T2 with the desired number of points.
% Type "doc linspace" (without the quotes) for more information.
x = linspace(T1, T2, noPoints);
% Create a 1D array from C1 to C2 with the desired number of points.
% Type "doc linspace" (without the quotes) for more information.
y = linspace(C1, C2, noPoints);
plot (x,y);
title('Time vs Concentration');
xlabel('Time [s]');
ylabel('Concentration');
% Update handles structure
guidata(hObject, handles);
Choose whatever option you wand (and understand better!). Personally, I would go for Option 2, but you choose. Let me know if it works for you.
  2 件のコメント
Bastion
Bastion 2011 年 8 月 10 日
Hey it works, big thanks!
However now I think I have stumble across something else, my T1 and T2 are only 2 numbers, so no matter what my equation is, it is always a straight line, not very helpful
I'm thinking say for example, T1 = 1 and T2 = 10, I'm thinking what code can I write so that it will display the concentration of all 10 points between 1 to 10.
I'm thinking something to do with the interval, eg. 1:1:10 but that can't be right. Do you have any ideas? Also how does 'doc linspace' work?
Arturo Moncada-Torres
Arturo Moncada-Torres 2011 年 8 月 11 日
I'm glad it worked.
Regarding your other question, post it in a new thread please. This is to keep the forum as neat as possible.

サインインしてコメントする。

その他の回答 (2 件)

Arnaud Miege
Arnaud Miege 2011 年 8 月 3 日
You need to add a set of axes to your GUI and plot the data on the axes. Have a look at GUI with Multiple Axes in the documentation for an example of this (using GUIDE).
HTH,
Arnaud
  1 件のコメント
Bastion
Bastion 2011 年 8 月 8 日
Thanx for the reply but the GUI with Multiple Axes is just too complicated for my level of understanding. Is there something easier?

サインインしてコメントする。


Bastion
Bastion 2011 年 8 月 10 日
I have looked through the GUI with the multi axes tutorial and I think the following line is what I need but can't understand a few of the code:
function plot_button_Callback(hObject, eventdata, handles, varargin)
% hObject handle to plot_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get user input from GUI
f1 = str2double(get(handles.f1_input,'String'));
f2 = str2double(get(handles.f2_input,'String'));
t = eval(get(handles.t_input,'String')); %%%%%%%%%%%%%%This line I don't understand %%%%%%%%
% Calculate data
x = sin(2*pi*f1*t) + sin(2*pi*f2*t);
y = fft(x,512);
m = y.*conj(y)/512; %%%Don't know how this formula works, any ideas? %%%
f = 1000*(0:256)/512; %%%Also don't understand this line %%%
% Create frequency plot in proper axes
plot(handles.frequency_axes,f,m(1:257)) %%%What is it plotting? %%%
set(handles.frequency_axes,'XMinorTick','on') %%%??? %%%
grid on
% Create time plot in proper axes
plot(handles.time_axes,t,x)
set(handles.time_axes,'XMinorTick','on')
grid on
Any help would be good

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by