Functions Matlab, how to modify?
16 views (last 30 days)
Show older comments
Jenny Andersen
on 24 Nov 2019
Commented: Jenny Andersen
on 25 Nov 2019
Hi,
so I have two vectors, Q = (a,l,m) and Z = (d,b,c). I have created a function to calculate Z, but the problem is that I don't want the answear to be d= something1, b=something2 and c= something3. I want it to be Z = =[something1, something2, something3] when I call the function.
function [a,l,m]=test1(d,b,c)
a=0;
l=b/sqrt(b^2 + c^2);
m=c/sqrt(d^2 + c^2);
a=a*1;
a=a*-1;
l=l*1;
l=l*-1;
m=m*1;
m=m*-1;
Is it possbible to modify the function in some way to achive this?
2 Comments
Turlough Hughes
on 24 Nov 2019
You would write
function Z = test1(d,b,c)
% operations to get a,l,m.
Z=[a l m];
end
KALYAN ACHARJYA
on 25 Nov 2019
Edited: KALYAN ACHARJYA
on 25 Nov 2019
function Z = test1(d,b,c)
a=0;
l=b/sqrt(b^2 + c^2);
m=c/sqrt(d^2 + c^2);
a=a*1;
a=a*-1;
l=l*1;
l=l*-1;
m=m*1;
m=m*-1;
Z=[a l m];
fprintf('Z=[%i %i %i]\n',Z);
end
Accepted Answer
Turlough Hughes
on 25 Nov 2019
Edited: Turlough Hughes
on 25 Nov 2019
Hi Jenny,
You could write your function to have two outputs as follows:
function [Z1,Z2] = test1(d,b,c)
% code to determine a,l,m
Z1=[a,l,m];
Z2=[a,-l,-m];
end
When calling the function you have to specify two outputs if you want them otherwise matlab does not know what to assign the second output to. Basically:
[Z1,Z2]=test1(d,b,c)
Another thing you could do would be to write a single output where you have 6 elements in a single output variable. That would go something like this:
function Z = test1(d,b,c)
% code to get a,l,m
Z=[a,l,m;a,-l,-m];
end
When you call the function now you get a single output with six elements that you are looking for.
I recommend looking at onramp tutorials. Especially part 7 on calling functions as well as vectors and matrices, part 3. Though, if you intend to use matlab much you are as well to go through all of the sections.
More Answers (1)
Jenny Andersen
on 25 Nov 2019
Edited: Jenny Andersen
on 25 Nov 2019
1 Comment
KALYAN ACHARJYA
on 25 Nov 2019
Edited: KALYAN ACHARJYA
on 25 Nov 2019
Why you have deleted your previous comments, its may confuse to the answer providers? This is answer section, please post your comment in comment section only.
See Also
Categories
Find more on Analog Devices ADALM1000 Support from Data Acquisition Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!