GUI derivative calculator, issues with coding portion

2 Ansichten (letzte 30 Tage)
William Fantin
William Fantin am 6 Mai 2019
Kommentiert: Murugan C am 8 Mai 2019
Hello All,
I am trying to do my final MATLAB project and it involves creating a GUI that can handle second order differential equations. Firstl I have been trying to figure out how to make a simple derivative GUI calculator, one that can handle something from the user say 4X and when a button is pushed will spit out its derivite, 4. So far I have made the GUI and have been trying to add some code. I am new to MATLAB and some of the errors are confusing. I will post parts of the code along with my logic below:
let me define some things to make it easier to understand.
I have an edit text box whose tag line is FCN (this is the box the user will enter the derivative that needs to be taken)
I have a push button whose tag line is equal
I have a static text box whose tag line is first (this is where the answer will pop out)
I wrote a few lines of code under the equal callback:
function equalPushButton_Callback(hObject, eventdata, handles)
% hObject handle to equalPushButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
syms x
a=get(handles.FCN,'string'));
b=str2sym(diff(a)); %Or b=double(b)?
set(handles.first,'string',sym2num(b));
I am wondering if someone can steer me in the right direction
Thanks
  3 Kommentare
William Fantin
William Fantin am 7 Mai 2019
Sure, so i did some more research and now my code is different. I have my program where if I type in x it will give the derivative of 1. But when I type say 4*x it will give an error. Heres the code:
function varargout = Test5(varargin)
% TEST5 MATLAB code for Test5.fig
% TEST5, by itself, creates a new TEST5 or raises the existing
% singleton*.
%
% H = TEST5 returns the handle to a new TEST5 or the handle to
% the existing singleton*.
%
% TEST5('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TEST5.M with the given input arguments.
%
% TEST5('Property','Value',...) creates a new TEST5 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Test5_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Test5_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 Test5
% Last Modified by GUIDE v2.5 05-May-2019 12:47:09
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Test5_OpeningFcn, ...
'gui_OutputFcn', @Test5_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 Test5 is made visible.
function Test5_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 Test5 (see VARARGIN)
% Choose default command line output for Test5
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Test5 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Test5_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 FCN_Callback(hObject, eventdata, handles)
% hObject handle to FCN (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 FCN as text
% str2double(get(hObject,'String')) returns contents of FCN as a double
% --- Executes during object creation, after setting all properties.
function FCN_CreateFcn(hObject, eventdata, handles)
% hObject handle to FCN (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 equalPushButton.
function equalPushButton_Callback(hObject, eventdata, handles)
% hObject handle to equalPushButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
f=get(handles.FCN,'String')'
syms x
der=diff(f,x)
set(handles.first,'String',char(der));
Thanks!!
Murugan C
Murugan C am 8 Mai 2019
Hi
I think, we shouldn't use "syms" for derivative .we have create one more edit box for getting derivative as like below,
f = get(handles.FCN,'String');
x = str2double(get(handles.derivative,'String')); % derivative order
der=diff(f,x);
set(handles.first,'String',char(der));

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by