MATLAB Answers

0

how to fix too many output argument?

Ma.Cathyrine Ravina さんによって質問されました 2019 年 1 月 4 日
最新アクティビティ Ma.Cathyrine Ravina さんによって コメントされました 2019 年 1 月 4 日
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 件のコメント

Geoff Hayes
2019 年 1 月 4 日
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?

サインイン to comment.

タグ

1 件の回答

GT
回答者: GT
2019 年 1 月 4 日
 採用された回答

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 件のコメント

Ma.Cathyrine Ravina 2019 年 1 月 4 日
Thank you so much. This really helps me a lot :D

サインイン to comment.



Translated by