MATLAB Answers

Bundle Funtions inside a Script

6 ビュー (過去 30 日間)
hegel
hegel 2016 年 1 月 28 日
コメント済み: hegel 2016 年 1 月 29 日
Hello, I wrote a script for parsing and plotting data. Also I wrote a bunch of specific functions in separate files to do these operations. I'd like to see if it is possible to define all these functions at the end of the script so that I don't need to keep track of so many files. It would be easier also if I wanted to give these files to somebody else, they would have everything in one single file. thanks in advance! -m

採用された回答

Stephen Cobeldick
Stephen Cobeldick 2016 年 1 月 28 日
編集済み: Stephen Cobeldick 2016 年 1 月 28 日
The easiest option is to turn the script into a function, and then put all of the other functions in the same Mfile. Doing this is easy, reliable, and it gives you exactly one simple Mfile.
With MATLAB it is not currently possible to mix scripts and functions in one Mfile, so you cannot simply place all of those function at the end of the script. But it is easy to convert a script to a function, and then you can put all of the other functions in the same file. The first function is the one that can be accessed externally by calling the its name, all of the other functions can be called by any function in that file (called local functions).
You should really be writing functions anyway: scripts are great for playing with code, but not for writing serious code (especially not code that you will be distributing to others!). This is because functions each have their own workspace, letting you encapsulate your algorithm without it being disturbed by the calling workspace, and vice versa.
  1 件のコメント
hegel
hegel 2016 年 1 月 29 日
thanks a lot, I'll try this.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 1 月 28 日
You should consider using the new facilities to 'Package App'
  1 件のコメント
hegel
hegel 2016 年 1 月 29 日
thanks for the info, I had forgotten about the apps but I'll use that for something else.

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

Community Treasure Hunt

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

Start Hunting!

Translated by