Is there a way to store my m code in a MAT file along with the data? If these were first on my path, it would provide a very convenient way to bundle my one-offs. Thanks!

1 view (last 30 days)
I'm sure this has been asked many times before. My usage over the past fifteen years has created a large code base true, but now I use Matlab everywhere. The only solution I have for keeping things together is to use the OS to make a folder, then to make mfiles for a particular problem set there. I have gone back an re-read the release notes since Matlab6, but I feel I must be missing something. Thanks

Accepted Answer

Clark Dunson
Clark Dunson on 26 Feb 2017
Really great answers, thanks!
I just did a review through a number of recent projects, and found that most of my one-offs are plot specializations, with only a few analysis hacks. Most of it I'd label some form of pasta, perhaps spaghetti-os. Perhaps rather than having plot methods on my data classes, I should create classes for plots. Hmmm.

More Answers (2)

Jan
Jan on 26 Feb 2017
Edited: Jan on 27 Feb 2017
No, this has not been asked before [EDITED: at least not many times]. It is a good programming practice to separate code and data, because this makes is easier to re-use code.
But of course it works. M-code files are just text files and you can store the contents together with the data:
function bundle(M, MAT)
% Input: M: Name of the M-function
% MAT: Struct containing the data or name of the MAT file.
Code = type([M, '.m']);
if ischar(MAT)
save(MAT, 'Code', '-append');
else
MAT.Code = Code;
save('Test.mat', 'MAT', '-struct');
end
Well, I admit I did not consider defining the name of the MAT-file, if a struct is used as input. But perhaps this helps you already to create the wanted function by your own.
I would not do this.

Walter Roberson
Walter Roberson on 26 Feb 2017
The current recommended approach is to Package an app . That puts everything related to the tool into one file for easy transportation and initialization.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!