Create and assign variable in function

조회 수: 2 (최근 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 CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by