how to fix too many output argument?
26 views (last 30 days)
Show older comments
Ma.Cathyrine Ravina
on 4 Jan 2019
Commented: Ma.Cathyrine Ravina
on 4 Jan 2019
I am working on a GUI wherein the users can input the mass, gravity and drag force and in return, it will graph the differential equation. Can you suggest any methods on how can i callback the string and put it into the equation? I tried using this:
a = str2num(get(handles.mass_second,'String'));
b = str2num(get(handles.mass_gravity,'String'));
c = str2num(get(handles.drag_second, 'String'));
function rk= f(t,y);
mass = a;
gravity =b;
rk= a*b - (c/a)*y;
end
but it gives me the error 'Too many output arguments'. I really want to learn matlab. thanks in advance :)
1 Comment
Geoff Hayes
on 4 Jan 2019
Ma - please copy and paste the full error message to this question? Also, are you nesting your f function within a callback (?)? How are you calling f?
Accepted Answer
Gareth
on 4 Jan 2019
There are several ways of doing this:
- Use a Live Script (with control slider bar)
- Create a MATLAB function and pass in the parameters you want
- Create a GUI (if using a newer version of MATLAB I would recommend app designer and package as an APP for others)
- Use the Symbolic Toolbox in MATLAB to calculate this (symbolicaly)
Please note I am using the latest version of MATLAB R2018b.
Live Script
Live Script is a MATLAB File that ends in mlx, which is designed for explaining concepts.
mass = 10;
gravity =9.5;
drag_second = 20;
t = 0:.1:10;
rk = mass*gravity - (drag_second/mass) * t;
plot(rk,t)
Please note that you would add the slider after mass, gravity and drag_second.
MATLAB Function
This is what you started to do and gave an error. As you need to pass in the variable, a, b.
function rk= myfunction(a,b);
t = 0:.01:10; % defining values of t, starting a 0 and going to 10, with 0.01 steps
rk= a*b - (c/a)*t;
end
Appdesigner
This is the new way MathWorks has of creating APPS, or User Interfaces.
- Type appdesigner in MATLAB
- Follow the tutorial
- Drag 3 Edit Numeric Fields into your canvas (give them the right name, e.g. mass,gravity,drag_second)
- Drag 1 Axes (for visualizition)
- Drag 1 Push button and call it Run (this is optional but easiest to get you going)
- Select Push button, right click it, Callback, Add PushButtonFcn Callback - this will execute your MATLAB Code when pushed.
- Add the following code:
mass = app.MassEditField.Value; %gets the numeric value from user interface
gravity = app.GravityEditField.Value;
drag_second = app.drag_secondEditField.Value;
t = 0:.01:10; % Calculate time vector same as before
rk = mass*gravity - (drag_second/mass) * t;
plot(app.UIAxes,rk); % updates the chart
If you are happy with your App, feel free to package it and share it with others.
Hope that this helps you get started.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!