execution order

What is the easiest way to control execution order of your code?
i have a function that creates an array of numbers and then I have a for loop that needs to use some numbers from that array. but the for loop seesms to be executing first.

4 件のコメント

Jonathan
Jonathan 2011 年 11 月 17 日
Can you give us an example of this to clarify your question?
Baba
Baba 2011 年 11 月 17 日
time=function(pwd); % creates an array of numbers;
for i=1:10
...
...
need to use parts of time array here
end
but it seems that the time array gets populated after the for loop is executed.
Walter Roberson
Walter Roberson 2011 年 11 月 17 日
No, in such a situation, the "time" variable would get populated before the "for" loop starts.
However, please remember that "time" is the name of a MATLAB library routine, and functions cannot be named "function". When you use library routine names as variables, it is common to run in to unexpected effects.
Baba
Baba 2011 年 11 月 17 日
thank you!

サインインしてコメントする。

 採用された回答

Jan
Jan 2011 年 11 月 17 日

0 投票

Matlab executes the code line by line in serial order. Inside a line the commands are executed from left to right.
Callbacks and timer functions are processed, if the command prompt is reached, or if PAUSE or DRAWNOW trigger their execution.
I do not see any possibilities to influence the order of execution.

7 件のコメント

Baba
Baba 2011 年 11 月 17 日
thank you!
Walter Roberson
Walter Roberson 2011 年 11 月 17 日
In modern versions, timer callbacks can occur between any two lines, apparently.
Jan
Jan 2011 年 11 月 17 日
@Walter: Is this documented?
Walter Roberson
Walter Roberson 2011 年 11 月 17 日
No, not documented :(
http://www.mathworks.com/matlabcentral/answers/10394-timer-and-interruptible-off-button-callback-priority-preemption
Oh wait, here is official evidence!
http://www.mathworks.com/support/solutions/en/data/1-2QJ375/index.html?solution=1-2QJ375
And I think I might have traced back the discussion I was thinking of:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/283728
Jonathan
Jonathan 2011 年 11 月 17 日
I would make a small modification to this answer. Inside a line, the commands are executed from left to right and inside to outside.
Daniel Shub
Daniel Shub 2011 年 11 月 17 日
@Walter, understanding timers is not that easy (and I am not sure you are implying it is easy). See my new question: http://www.mathworks.com/matlabcentral/answers/21537-what-thread-do-timers-operate-in
Jan
Jan 2011 年 11 月 17 日
@Jonathan: Exactly. Inside a line the precedence order rules, but for the same precedence the left operation is performed at first.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2011 年 11 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by