Create and assign variable in function
2 次查看(过去 30 天)
显示 更早的评论
Hello I have a function and I pass in a row vector with different values and I want to take those values and assign variables that I use throughout my function to those specific values and also make them global to be used in other functions
examples
function [] = example1(theta)
if (nargin<1)
theta = zeros(1,28)
sym('j%d',size(theta)%I also tried genvarname({'j'})
for i=1:length(theta)
j{i} = theta(i);
end
else
sym('j%d',size(theta)%I also tried genvarname({'j'})
for i=1:length(theta)
j{i} = theta(i);
end
end
global j1 j2 ....j28 %is there a better way to do this line?
j1
j2
.
.
j28
When I run the function it says that j1-j28 are undefined, what I am expecting is it to print out j1 = 0 -j28 = 0 if I didn't put in any arguments and j1 = 5 - j28 = 7 if I entered values in for theta
and also I want to be able to use j1-j28 in another function
function exmaple2()
plot(sin(j1))
end
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!