How to plot two plots in different axes on matlab GUI?

15 ビュー (過去 30 日間)
William Fantin
William Fantin 2019 年 5 月 18 日
コメント済み: Adam Danz 2019 年 5 月 18 日
Hello,
Below I have posted my code. My program evaulates a spring mass system by allowing users to input initial conditions along with the mass,spring constant k, any external forces, and any damping present. My program then solves the differential equation and outputs the answer, then I graph the result.
I have also created an animation that involves a spring and a ellipse/circle that mimics an actual spring mass. I have modeled my animation so it moves with respect to the actual equation (sorry if thats a bit unclear). I just figured out how to "draw" my graph out with a for loop (line 195 of my code)(just realized that you cannot see the numbers so the line of code on line 214 is: for i=1:length(time)). And below starting on line 214 I start my loop which draws my animation, and that code starts:for k=V(1):5:length(V)-1 .
The issue I am having is I want both to be graphed/drawn out at the same time, so you could actually see the spring oscillating while the graph is being drawn out. I feel like it would make the presentation much better.
Any help would be appreciated!!!
Thanks, code below:
function varargout = SpringMassP(varargin)
% SPRINGMASSP MATLAB code for SpringMassP.fig
% SPRINGMASSP, by itself, creates a new SPRINGMASSP or raises the existing
% singleton*.
%
% H = SPRINGMASSP returns the handle to a new SPRINGMASSP or the handle to
% the existing singleton*.
%
% SPRINGMASSP('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SPRINGMASSP.M with the given input arguments.
%
% SPRINGMASSP('Property','Value',...) creates a new SPRINGMASSP or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before SpringMassP_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to SpringMassP_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help SpringMassP
% Last Modified by GUIDE v2.5 17-May-2019 11:09:42
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @SpringMassP_OpeningFcn, ...
'gui_OutputFcn', @SpringMassP_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before SpringMassP is made visible.
function SpringMassP_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to SpringMassP (see VARARGIN)
% Choose default command line output for SpringMassP
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes SpringMassP wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = SpringMassP_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
function mass_Callback(hObject, eventdata, handles)
% hObject handle to mass (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of mass as text
% str2double(get(hObject,'String')) returns contents of mass as a double
% --- Executes during object creation, after setting all properties.
function mass_CreateFcn(hObject, eventdata, handles)
% hObject handle to mass (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% 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
function sprconst_Callback(hObject, eventdata, handles)
% hObject handle to sprconst (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of sprconst as text
% str2double(get(hObject,'String')) returns contents of sprconst as a double
% --- Executes during object creation, after setting all properties.
function sprconst_CreateFcn(hObject, eventdata, handles)
% hObject handle to sprconst (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% 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
function damp_Callback(hObject, eventdata, handles)
% hObject handle to damp (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of damp as text
% str2double(get(hObject,'String')) returns contents of damp as a double
% --- Executes during object creation, after setting all properties.
function damp_CreateFcn(hObject, eventdata, handles)
% hObject handle to damp (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% 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
function fext_Callback(hObject, eventdata, handles)
% hObject handle to fext (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of fext as text
% str2double(get(hObject,'String')) returns contents of fext as a double
% --- Executes during object creation, after setting all properties.
function fext_CreateFcn(hObject, eventdata, handles)
% hObject handle to fext (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% 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
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%Below section deals with all the inputs from the user, graphs the
%results, and displays the equation.
syms x m u k f x(t)
f=str2sym(get(handles.fext,'String'))%Converting the input vars to symbolic
m=str2sym(get(handles.mass,'String'))
u=str2sym(get(handles.damp,'String'))
k=str2sym(get(handles.sprconst,'String'))
Dx=diff(x) %Defines Dx to be the first derivative of x
D2x=diff(x,2) %Defines D2x to be the second derivative of x
ode=m*D2x+u*Dx+k*x==f %Defining my differential spring mass equation to be "ode"
ICx=str2num(get(handles.initialposition,'String')) %Allowing user to enter initial conditions
ICv=str2num(get(handles.initialvelocity,'String'))
a=dsolve(ode,x(0)==ICx, Dx(0)==ICv)
set(handles.answer,'String',char(a));
axes(handles.axes2)
[time,position]=fplot(a,[0 5]);
%I want the below loop to run while the other graph is being drawn on
%handles.animation. Not quite sure how to do this..
%Somehow I need to get the below loop to plot what it is trying to plot in
%tandem with the plot for handles.animation starting on line 214
for i=1:length(time)
plot(time(i),position(i),'x')
hold on
plot(time(1:k),position(1:k))
pause(.001)
end
%plot(time,position)
fplot(a,[0 5]); %one output will be the x axis and one will be the y. once we specify the function it will
Q=time; %This is the time array of the function graphed
V=position %This is the position array of the function graphed
%G=V(end);
%x0=sum(logical(diff(sign(V)))); %This obtains the number of times the
%graph crosses the origin, but i dont need that
axes(handles.Animation)
%A1=V(1); %I dont need these two
%B1=length(V);
y=V; %V is the position array of the graph that is graphed in axes2
for k=V(1):5:length(V)-1 %My loop is going from the first entry of V array, to the last entry,
hold on
A3=mean(V);
J=round(A3)
P=circular(y(k),.5,.2,'r')
xleft=min(V);
xright=max(V);
axis([xleft xright -2 4])
X=[xleft,xleft+((V(k)-xleft)/9),xleft+2*((V(k)-xleft)/9),xleft+3*((V(k)-xleft)/9),xleft+4*((V(k)-xleft)/9),xleft+5*((V(k)-xleft)/9),y(k)]
Y=[.5,1,0,1,0,1,.5]
plot(Y,X,'b','linewidth',1) %I changed these to plot y,x so we can have a vertical graph
set(gca,'Ydir','reverse')
pause(.2)
P=circular(y(k),.5,.2,'w-')
plot(Y,X,'w','linewidth',1)
%set(gca,'Ydir','reverse')
end
%r is the radius of the circle
%y(k) is the k'th entry of y which is an array of numbers that differ
%depending on what iteration we are in in our for loop
%X=[xleft,((V(k)-xleft)/7),2*((V(k)-xleft)/7),3*((V(k)-xleft)/7),4*((V(k)-xleft)/7),5*((V(k)-xleft)/7),y(k)]
%Y=[0,2,0,2,0,2,1]
%X=[0,0+k,2*k,3*k,4*k,5*k,6*k,7*k]
%Y=[0,2,0,2,0,2,0,1]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%plot(P)
% if y(k)>0
% plot([y(k),1+y(k),1+y(k),y(k),y(k)],[0,0,2,2,0,],'r-') %Plotting one side of box based on position
% plot([y(k),y(k+1)*A3,y(k+1)*A3,y(k),y(k)],[0,0,2,2,0,],'r-')
%plot([y(k),y(k+1)/A3,y(k+1)/A3,y(k),y(k)],[0,0,2,2,0,],'r-')
% axis([-2 2 -1 1])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plot([y(k),1+y(k),1+y(k),y(k),y(k)],[0,0,2,2,0,],'w-')
% end
%if y(k)<0
% plot([y(k),-1+y(k),-1+y(k),y(k),y(k)],[0,0,2,2,0,],'r-')
% xleft=min(V);
% xright=max(V);
% axis([xleft xright -2 4])
%pause(.02)
%plot([y(k),-1+y(k),-1+y(k),y(k),y(k)],[0,0,2,2,0,],'w-')
%end
% plot([y(k),y(k+1)*A3,y(k+1)*A3,y(k),y(k)],[0,0,2,2,0,],'w-')
%plot([y(k),y(k+1)/A3,y(k+1)/A3,y(k),y(k)],[0,0,2,2,0,],'w-')
%if y(k)>0
% for c=A1:B1
% plot(sin(x))
%Plot sin x from 0 to 2pi, then we can move it along the x axis
%wrt to edge of box, then change the frequency or period of
%sine graph.
% X=[0,0+k,2*k,3*k,4*k,5*k,6*k,7*k];
% Y=[0,2,0,2,0,2,0,1];
% hold on
% axis([-2 20 -2 5])
% plot(X,Y,'b','linewidth',2)
% pause(.2)
% plot(X,Y,'w','linewidth',2)
%take a starting point and make a spring ang box lets say we have a for
%loop with step for step 1:20 inside loop draw spring and also draw box
%and we will pause and we will draw spring and box whited out, to draw
%spring it willl be a functionthe function will be spring(xfinal) the
%function will be like my script file for a spring, we could have a
%second parameter so it will be function spring (xfinal,color) when we
%wsolve the DE it will give us x(t), , so the step in the for loop is
%us so its for step=a:length(x(t)), another way is 1:10:length(x(t))
%how far it will go is max of the position. so we need to know how many
%times it goes from positive to negative, how many times it changes
%signs
% if
% end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%y=A1:B1%G should really be max(position)
%for k=1:G
% hold on
%axis([0 25 -5 5])
% plot([y(k),y(k+1),y(k+1),y(k),y(k)],[0,0,2,2,0,],'r-')
% pause(.2)
%plot([y(k),y(k+1),y(k+1),y(k),y(k)],[0,0,2,2,0,],'w-')
%end
%for k=1:G
%X=[0,0+k,2*k,3*k,4*k,5*k,6*k,7*k]
%Y=[0,2,0,2,0,2,0,1]
%hold on
%axis([-2 20 -2 5])
%plot(X,Y,'b','linewidth',2)
%pause(.2)
%plot(X,Y,'w','linewidth',2)
%end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function answer_CreateFcn(hObject, eventdata, handles)
% hObject handle to answer (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject handle to checkbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox1
checkboxValue = get(handles.checkbox1, 'value')
if checkboxValue
set(handles.text19, 'Visible', 'on');
else
set(handles.text19, 'Visible', 'off');
end
% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu2
% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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
function initialposition_Callback(hObject, eventdata, handles)
% hObject handle to initialposition (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of initialposition as text
% str2double(get(hObject,'String')) returns contents of initialposition as a double
% --- Executes during object creation, after setting all properties.
function initialposition_CreateFcn(hObject, eventdata, handles)
% hObject handle to initialposition (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% 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
function initialvelocity_Callback(hObject, eventdata, handles)
% hObject handle to initialvelocity (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of initialvelocity as text
% str2double(get(hObject,'String')) returns contents of initialvelocity as a double
% --- Executes during object creation, after setting all properties.
function initialvelocity_CreateFcn(hObject, eventdata, handles)
% hObject handle to initialvelocity (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% 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

回答 (0 件)

カテゴリ

Help Center および File ExchangeGeneral Applications についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by