Create and assign variable in function

1 回表示 (過去 30 日間)
Ryan
Ryan 2014 年 3 月 28 日
編集済み: Azzi Abdelmalek 2014 年 3 月 28 日
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 件)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by