Using Vector Components in Functions
이전 댓글 표시
How can I use a vector component from a previously defined vector variable in a function? I do not want to redefine the vector variable inside the function!
X is previously loaded into the script using
load('TrussDesign.mat', 'C', 'Sx', 'Sy', 'X', 'Y', 'L' );
and my function is:
function xcoeff = xcoeff(a,b)
A = X(b) - X(a);
B = Y(b) - Y(a);
r = sqrt((A^2)+(B^2));
d = X(a) - X(b);
xcoeff = d/r;
end
I know I can do:
function xcoeff = xcoeff(a,b)
X = cell2mat(struct2cell(load('TrussDesign.mat','X'))); % X positions of joints
Y = cell2mat(struct2cell(load('TrussDesign.mat','Y'))); % Y positions of joints
A = X(b) - X(a);
B = Y(b) - Y(a);
r = sqrt((A^2)+(B^2));
d = X(a) - X(b);
xcoeff = d/r;
end
but the whole point of this question is seeing if I can avoid that so I don't have to change the code if I choose to load a different file in the beginning of the script.
댓글 수: 2
Jan
2021년 3월 24일
Just a question: cell2mat(struct2cell(load()))? What about:
Data = load(File, 'X');
X = Data.X;
% or
X = getfield(load(File, 'X'), 'X');
Grace Chabbott
2021년 3월 24일
편집: Grace Chabbott
2021년 3월 24일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Desktop에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!