My files are a mess, please help

2 ビュー (過去 30 日間)
jlt199
jlt199 2016 年 8 月 23 日
編集済み: José-Luis 2016 年 8 月 25 日
I've been in my job 3 months and already seem to have an unmanageable number of m-files (both functions and scripts). I'm looking for advice to keep them in some sort of order for example what names do you give your files, how do you store revisions and make sure you are using the most current version? How do you distinguish between a script and a function, what does your file tree look like?
Currently because I'm working on a single project, my names are all variations on a theme that I know if I were to go away for a month and come back I'd struggle working out which functions to use
  3 件のコメント
jlt199
jlt199 2016 年 8 月 24 日
Would this prevent me having to have readtext_1, readtext_2 etc.?
José-Luis
José-Luis 2016 年 8 月 25 日
編集済み: José-Luis 2016 年 8 月 25 日
Yes, source control would keep track of all historic (read commited) file modifications.

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

回答 (3 件)

KSSV
KSSV 2016 年 8 月 24 日
1. The first and foremost thing to remember what you have done/ coded in the file is to write comments. It is a good practice to include comments, so that you can remember what you have done in the code.
2. Include purpose of the code at the begining, so that it can be printed in MATLAB using 'help filename'. It prints the purpose of the code. It makes easy to distinguish the code from others.
3. Always maintain different folders for each work/ project.
4. Try to name the files with the word closer to what code does. Eg. If you are writing a code for interpolation, try to name it so that it conveys interpolation. Remember the the file you name should not be a inbuilt matlab function.
5. You can distinguish between script and function by looking the files in current folder using MATLAB. It will show functions as 'fx' and scripts with matlab logo.

Bjorn Gustavsson
Bjorn Gustavsson 2016 年 8 月 24 日
In addition to the above answer I'd like to add a few more points.
  1. you write functions for a couple of reasons, one of the main is that they should be reusable - making future work easier. For that reason put them in some directory together with conceptually/topically related function, add the directory to your matlab-path and put a Contents.m file in that directory with the first comment-line of each function. This makes it possible to get a quick overview of the contents of that toolbox. Look at some matlab toolbox for examples of how Mathworks does this.
  2. Don’t mix scripts into such toolboxes.
  3. The way I see it scripts are written to run the programs that solves tasks for a project, functions are tools written to do things over and over again. So keep scripts in project-specific directories separate from your toolboxes with all functions.
  4. Use a tool like M2HTML to maintain documentation of your code.
  5. Rewrite functions and scripts. First solutions (in my experience) are typically "not all that good".
HTH

jlt199
jlt199 2016 年 8 月 24 日
Thanks for your suggestions, I have downloaded GIT and will hopefully figure out what it does in due course.
I like the idea of creating folders with toolboxes and separating functions from scripts. I would like some more advice about making functions reusable. Mine are generally very long and very specific.
The naming and comments suggestions are also very helpful and something I need to get better at.
  1 件のコメント
Thorsten
Thorsten 2016 年 8 月 24 日
If you can identify similar parts in your code that you copy and paste with small variations, than you can consider it making a function.

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

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by