Save text of script in .mat file?

21 Ansichten (letzte 30 Tage)
Peter Frame
Peter Frame am 16 Okt. 2023
Kommentiert: Stephen23 am 18 Okt. 2023
Is there a way of saving the text of the current script as a variable? I'd like to save this to a .mat file along with the other workspace variables created by the script. Even better, I'd like to save the current version of all user-written functions called by the script in .mat file.
Some context for why I want to do this:
I frequently run simulations in MATLAB and save the data to a .mat file, then change a couple of parameters in the simulation, or tweak the code slightly, and repeat. When the time comes to use the data, it is often hard to keep track of how each .mat file was generated. The parameter values are saved as long as I save the entire workspace, but what they mean --- or more generally, what the code was doing --- is not. I usually keep a log with notes about each .mat file to help me remember this information, but this is cumbersome and incomplete. It seems that saving the script as part of the .mat file would be an easier and more complete way to keep all the information about the data. I imagine this is a very common problem, so I'm wondering if there is a way to do this, or otherwise, if anyone has a good strategy for keeping the info about the data.
Thanks,
Peter
  3 Kommentare
Peter Frame
Peter Frame am 16 Okt. 2023
So as not to clutter the folder with many versions of the same script. Also, because ideally the script information would be saved automatically and in the same place as the data generated from it.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Les Beckham
Les Beckham am 16 Okt. 2023
I suppose that one option would be to read the file contents into a string array and save that to your mat file.
For example:
str = readlines('tic.m') % replace 'tic.m' with mfilename to read the current file
str = 27×1 string array
"%TIC Start a stopwatch timer." "% TIC and TOC functions work together to measure elapsed time." "% TIC, by itself, saves the current time that TOC uses later to" "% measure the time elapsed between the two." "%" "% TSTART = TIC saves the time to an output argument, TSTART. The" "% numeric value of TSTART is only useful as an input argument" "% for a subsequent call to TOC." "%" "% Example: Measure the minimum and average time to compute a sum" "% of Bessel functions." "%" "% REPS = 1000; minTime = Inf; nsum = 10;" "% tic;" "% for i=1:REPS" "% tstart = tic;" "% sum = 0; for j=1:nsum, sum = sum + besselj(j,REPS); end" "% telapsed = toc(tstart);" "% minTime = min(telapsed,minTime);" "% end" "% averageTime = toc/REPS;" "%" "% See also TOC, CPUTIME." "" "% Copyright 1984-2008 The MathWorks, Inc." "% Built-in function." ""
  2 Kommentare
Peter Frame
Peter Frame am 16 Okt. 2023
Perfect, thanks!
Les Beckham
Les Beckham am 16 Okt. 2023
You are quite welcome.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Steven Lord
Steven Lord am 16 Okt. 2023
Rather than trying to put (potentially a large amount of) code in your MAT-file, why not create a struct or object with your parameters (with descriptive field and/or property names), pass that struct or object into your function, and have the function return a struct or object with the results of the analysis for those parameter values inside it?
  1 Kommentar
Peter Frame
Peter Frame am 16 Okt. 2023
Hi Steven,
That would be fine if I never changed the function, only the parameters (and knew that I had not changed it). In practice, I will often tweak the script (function in your suggestion) and it's easy to forget if changes have been made that will influence the interpretation of the results. Making these kind of changes, e.g., changing a hard coded parameter, or changing the window function I'm using in ffts, is likely not best practice, but I've certainly done it. When I go back and access the data in a year, I want to know exactly how it was created without trying to remember if I tweaked the script in the mean time.
Saving the script as a receipt is a way of doing this. While it might be a lot to look through, it's good to have, and it takes up an insignificant amount of storage relative to the data itself.
Peter

Melden Sie sich an, um zu kommentieren.


John D'Errico
John D'Errico am 16 Okt. 2023
Is it really a common problem? Not that I've ever seen any need for in 35 years of MATLAB use, nor in any question I can recall in almost as long a time spent answering questions in various forums.
This seems like a pain in the butt to do. Because then accessing the file in some sort of form you will store it, which in itself will not be very usable will not be trivial.
Why not just use a revision control system? Now all you need to save with the results is a version number. Instead of scripts, use functions. So now all you need to save are the parameters you will pass into those functions. Scripts are bad news anyway.
  1 Kommentar
Peter Frame
Peter Frame am 17 Okt. 2023
Thanks for the wisdom, John.
I agree that version control is the more diciplined way of ensuring you know exactly what code was run, but I think what I'm describing is not so cumbersome and does the job. All you need is the line that Les provided before saving the worspace, and you have exactly the code that was run (provided you do not change other functions that it calls).

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by