Hello,
I am facing a problem with building an array using a custom function.
I would like to transfer a task from the main script file to the function file. I have such a piece of script:
StageMain=0;
TStart=10;
TEnd=500;
TStep=0.001;
Tmag=1;
.
.
.
DataInput = ones(length(StageMain),8);
Tarray = ones(1,length(StageMain));
.
.
.
T=TStart;
while T<TEnd
T=T+TStart*StageMain;
StageMain=StageMain+TMag;
DataInput(StageMain,1) = double(T);
Tarray(1, StageMain) = double(T);
end
its job is to build (actually update) two arrays DataInput and Tarray. Now I am trying to put this in function file which also will give me two updated arrays.
function [DataInput, Tarray] = arraybuilder(TStart, TEnd, TStep, TMag, StageMain)
T=TStart;
while T<TEnd
T=T+TStep*StageMain;
StageMain=StageMain+TMag;
DataInput(StageMain,1) = double(T);
Tarray(1, StageMain) = double(T);
end
however, it only builds one vertical array that has double-length and the first part is filled with 0 and the second one with correct answers for DataInput
Thank you for any ideas.

댓글 수: 5

Roger J
Roger J 2020년 7월 20일
편집: Roger J 2020년 7월 20일
I made the following function:
function [DataInput, Tarray] = arraybuilder(TStart, TEnd, TStep, TMag, StageMain)
T=TStart;
while T<TEnd
T=T+TStep*StageMain;
StageMain=StageMain+TMag;
DataInput(StageMain,1) = double(T);
Tarray(1, StageMain) = double(T);
end
and then called it from the commnad line with:
[X Y] = arraybuilder(10,500,.001,1,0)
and the answer shows that both X and Y are 991 non-zero elements, and X(i) == Y(i) for all i.
Ok thanks, but for this set
[X Y] = arraybuilder(3.372510000000000e+02,380,.001,1,0)
it makes no sense and in main file it does.
Roger J
Roger J 2020년 7월 20일
편집: Roger J 2020년 7월 20일
when I run with
[X Y] = arraybuilder(3.372510000000000e+02,380,.001,1,0)
I get X and Y with 293 non-zero elements, and X(i) == Y(i) for all i.
I'm still not seeing the problem that you described.
Ok, that is just my stupidity, I was calling it using "pure" arraybuilder(3.372510000000000e+02,380,.001,1,0) and expecting to get two outputs. After calling it like you did it [X Y] it works like a charm.
Thank you.
Roger J
Roger J 2020년 7월 20일
Good! We've all been there. Mark the answer accepted, if you can.

댓글을 달려면 로그인하십시오.

 채택된 답변

Roger J
Roger J 2020년 7월 20일

0 개 추천

If you don't assign the return values from a function call, then only the first is assigned to the default
ans
variable.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Financial Toolbox에 대해 자세히 알아보기

제품

릴리스

R2019a

질문:

2020년 7월 20일

댓글:

2020년 7월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by