how to fix too many output argument?

26 visualizaciones (últimos 30 días)
Ma.Cathyrine Ravina
Ma.Cathyrine Ravina el 4 de En. de 2019
Comentada: Ma.Cathyrine Ravina el 4 de En. de 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;
but it gives me the error 'Too many output arguments'. I really want to learn matlab. thanks in advance :)
  1 comentario
Geoff Hayes
Geoff Hayes el 4 de En. de 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?

Iniciar sesión para comentar.

Respuesta aceptada

GT el 4 de En. de 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;
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;
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 comentario
Ma.Cathyrine Ravina
Ma.Cathyrine Ravina el 4 de En. de 2019
Thank you so much. This really helps me a lot :D

Iniciar sesión para comentar.

Más respuestas (0)


Más información sobre Programming en Help Center y File Exchange.


Community Treasure Hunt

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

Start Hunting!

Translated by