-1

How can I tell / export / copy the current M file being run to a directory. I know about the copy command I'm just not sure how to get the current M file that is being run.

The reason for this is that I try out the same M files with different edits that create various output files. And I would like to keep the M file changes with the exported files it creates together.

Thanks

PS: I'm using octave 3.8.1 which is like matlab

Rick T
  • 3,349
  • 10
  • 54
  • 119

4 Answers4

4

Use mfilename

p = mfilename('fullpath')

The fullpath option returns the complete path, which directly allows you to use it with the copy command.

Daniel
  • 36,610
  • 3
  • 36
  • 69
3

Daniel's solution answers your immediate question, but perhaps there is a better way to organize your workflow.

Maybe in addition to creating the output files, you can export a "parameters" file alongside the outputs which describe all parameters needed to recreate the experiment. This can be as simple as calling save to create a MAT-file containing necessary variables... Just an idea :)

Amro
  • 123,847
  • 25
  • 243
  • 454
0

My suggestion is to write a script in whatever shell you have available to invoke octave with your M-file where you also copy your M-file and output.

https://www.gnu.org/software/octave/doc/interpreter/Command-Line-Options.html

Per M.
  • 95
  • 1
  • 11
0

Thanks Daniel

This code may help someone else that needs to do this

currentfile=strcat(mfilename('fullpath'),'.m') %current file being run with path
[pathstr,name,ext] = fileparts(currentfile) % split into parts
currentfilecpyto=strcat('/tmp/A_',name,ext) %where to copy file to
copyfile(currentfile, currentfilecpyto) %copy file
Rick T
  • 3,349
  • 10
  • 54
  • 119