how to fix too many output argument?

23 Ansichten (letzte 30 Tage)
Ma.Cathyrine Ravina
Ma.Cathyrine Ravina am 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 Kommentar
Geoff Hayes
Geoff Hayes am 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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Gareth
Gareth am 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.
  1. Type appdesigner in MATLAB
  2. Follow the tutorial
  3. Drag 3 Edit Numeric Fields into your canvas (give them the right name, e.g. mass,gravity,drag_second)
  4. Drag 1 Axes (for visualizition)
  5. Drag 1 Push button and call it Run (this is optional but easiest to get you going)
  6. Select Push button, right click it, Callback, Add PushButtonFcn Callback - this will execute your MATLAB Code when pushed.
  7. 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.
  1 Kommentar
Ma.Cathyrine Ravina
Ma.Cathyrine Ravina am 4 Jan. 2019
Thank you so much. This really helps me a lot :D

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu App Building finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by