How we can skip copying codes ?

1 回表示 (過去 30 日間)
yogesh jain
yogesh jain 2016 年 1 月 17 日
編集済み: Stephen23 2016 年 1 月 18 日
Hello all, Many of the places I am using same lines of code , I don't want to copy codes all over again and again . What should I do ?

採用された回答

Stephen23
Stephen23 2016 年 1 月 17 日
編集済み: Stephen23 2016 年 1 月 17 日
Write functions.
Instead of writing/copying code into lots of different locations, write a function to perform each task and call it from each location where it is needed.
Scripts are great for trying something out or for playing around with some data, but if your code is going to hang around for more than a day then turn them into functions. Functions are also used to allow code to be written (and maintained) in one location, rather than repeating it everywhere.
  2 件のコメント
yogesh jain
yogesh jain 2016 年 1 月 18 日
編集済み: yogesh jain 2016 年 1 月 18 日
I don't want any input / output arguments . Just want instantiation of code . Should I use class-object for that ? Thank you
Stephen23
Stephen23 2016 年 1 月 18 日
編集済み: Stephen23 2016 年 1 月 18 日
You can define arguments if you need them, but actually functions don't need input or output arguments at all:
function myfun % <- see, no arguments
... do something
end
Note that each function has its own workspace. If you are planning on passing any values between workspaces then passing arguments is the most efficient way of doing this. Nested functions are also very efficient. Do not use globals (there are plenty of discussions on this forum that will tell you why global is a bad way to program).

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by