Computed values of a function reiterates in Command window.

Hello, I have the following problem. After running this function (parameters(1:30))
function [p, v, a] = parameters(t)
for i=1:numel(t)
if t(i)>0 & t(i)<=10
p(i)=0.5.*t(i).^2;
elseif t(i)>=10 & t(i)<=20
p(i)=0.05.*t(i).^3-t(i).^2+15.*t(i)-50;
else t(i)>=20 & t(i)<=30
p(i)=0.0025.*t(i).^4-0.15.*t(i).^3+135.*t(i)-1650;
end
for j=1:numel(p)
v(j)=p(j)/t(j)
end
for n=1:numel(v)
a(n)=v(n)/t(n)
end
end
values of a and v reiterates in command window many times. I would like to get answer in command window without any reiteration of computed values. Can somebody help me with this?

 Accepted Answer

Terminate the lines with a semicolon (;) to suppress the Command Window output:
v(j)=p(j)/t(j);
a(n)=v(n)/t(n);

6 Comments

Hello, thank you. But when I terminate lines with semicolon, the values of p appears in command window only.
My pleasure.
I do not understand. All your other lines are terminated by ;, and I do not see a specific assignment that would output ‘p’.
If you want, you can eliminate both the ‘v’ and ‘a’ loops with:
v = p./t;
a = v./t;
This takes advantage of element-by-element array operations. For a full description, see: Array vs. Matrix Operations.
Ok. But still I can not get values of a and v in work space. I get only ans which contains computed values of p. Do you now how to get values of a and v in work space?
Sergey
They are local to the workspace of your ‘parameters’ function, so you will not be able to display them in tooltips, for instance. You return them as outputs from your function, so they should all be available in your script workspace, if you call it as:
[p, v, a] = parameters(t);
If you call it as:
z = parameters(t);
it will only return the first output, ‘p’ (in variable ‘z’ in this illustration), not all three. You have to ask for all three to be returned in your function call to ‘parameters’ if you want all of them.
Now I got everything I wanted.
Thank you so much!!!
Sergey
As always, my pleasure!
Save the polar bears!

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!