This question already has answers here:
Find location of current m-file in MATLAB
(4 answers)
Closed 6 years ago.
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
Use mfilename
p = mfilename('fullpath')
The fullpath option returns the complete path, which directly allows you to use it with the copy command.
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 :)
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
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
Related
I'm working on my MATLAB code in a number of different locations, and it would be really helpful if I could make the code aware of its location on the computer. Till now I worked with .m-files. For .m-files I found the following solutions:
%example 1
cd(fileparts(mfilename('fullpath')))
or
%example 2
tmp = matlab.desktop.editor.getActive;
cd(fileparts(tmp.Filename));
or
%example 3
S = dbstack('-completenames');
S(1).file
or
%example 4
which(mfilename)
But with MATLAB 2016a there comes a new feature called live script. And with that those solutions are not working anymore.
%For example I would like to do something like this
cd(MLX_FILELOCATION);
%or
which(mlxfilename)
(Edit III: Problem: I am not able to get the path/filelocation or name of the current opened/executed MATLAB-file. With *.m-files this is possible with the examples above. With *.mlx-files it is not possible anymore. And I prefere to use *.mlx-files instead of *.m-files.)
Outputs of the examples above executed in a *.mlx-file:
%example1: mfilename returns the path to the 'MatlabEvaluationHelper' in the 'AppData\Local\Temp'-folder
%example2: output is an empty array
%example3: same output as example1
%example4: same output as example1, because mfilename returns "MatlabEvaluationHelper"
Edit I:
My first goal is that I would like to change the "current folder" (-> "cd") to the path of the running script.
Why: In the same folder with the mlx-file I have for example .csv-files with data. And for example by tomorrow I have new folder. I copy the mlx_file and now I want to make sure that I don't use the csv-files from yesterday (because the current folder from yesterday is shown in the file browser of MATLAB) -> so I would like to change the "current folder" automatically with just copying the mlx-file into a new folder.
If there is a better practise for that, please let me know.
Thanks for helping
Edit II:
Example for a used workflow:
I programmed a MATLAB script. Saved it in folder "Dataset_ONE". Furthermure I copy "Dataset_ONE.csv"-file into the same folder. E.g. I now create a plot and save it as the "*.png" in folder "Dataset_ONE".
The day after I might have a second (a new and with that different) dataset "Datasset_TWO". I create a new folder "Dataset_TWO". Copy the MATLAB-files to the new folder. Open the MATLAB file there. Then, because of the default settings, MATLAB has changed the "Current Folder" to the new folder where I opened MATLAB.
But if I now open the MATLAB script in the first folder again (at the same time with the other dataset MATLAB script) I have to be careful about the current folder.
In this case it might be useful to have the described solution.
If what you want is some sort of way of preventing you running the wrong script on the wrong data without realising, then you could add a safety instruction at the top of each script, throwing an error if your current directory is not the same as the location of the script you're running. e.g.
>> assert (strcmp (pwd, '/absolute/path/to/my/script'));
As for loading the right data / saving to the right location, just load and save using absolute paths and there should be no confusion.
This question already has an answer here:
How to Set Path in Matlab?
(1 answer)
Closed 7 years ago.
I have created my own mfile and would like to add it permanently lie built in functions so that I dont have to specify path. Is there a way to do it?
You should store your .m file (either script or function) in a folder belonging to the MatLab search path.
To add your own folder to the MatLab search path you can use eiterh:
the Set Path tool, available on the MatLab Home toolbar
use the buil-in function path (from the Command Window or, for example, as part of a script)
E. g.
path(path,'c:\my_folder\my_sub_folder')
Hope this helps.
Write your file and save it and then add the containing folder to the MATLAB search path. You do not need to look for it every time.
addpath('c:/matlab/myfiles')
Note: your filename should not exist within MATLAB, otherwise your file would be the default one, when you call that command.
You can also do most of the work from command-line:
First, create a folder,
mkdir('c:/matlab/myfiles')
Add it to the top of your search path,
addpath('c:/matlab/myfiles')
and then save the search path for future MATLAB sessions
savepath c:/matlab/myfiles/pathdef.m
I've downloaded a data set that I am interested in. However, it is in .mat format and I do not have access to Matlab.
I've done some googling and it says I can open it in SciLab.
I tried a few things, but I haven't found any good tutorials on this.
I did
fd = matfile_open("file.mat")
matfile_listvar(fd)
and that prints out the filename without the extension. I tried
var1 = matfile_varreadnext(fd)
and that just gives me "var1 = "
I don't really know how the data is organized. The repository described the data it contains, but not how it is organized.
So, my question is, what am I doing wrong in extracting/viewing this data? I'm not committed to SciLab, if there is a better tool for this I am open to that.
One options is to use Octave, which can read .mat files and run most Matlab .m files. Octave is open source with binaries available for Linux, Mac, and Windows. Inside of Octave you can load the file using:
load file
See Octave's manual section 14.1.3 Simple File I/O for more details.
In Scilab:
loadmatfile('file.mat');
(Source)
I had this same interest a few years back. I used this question as a guide. It uses Python and SciPy. There are options for NumPy and hd5f as well. Another option is to write your own reader for the .mat format in whatever language you need. Here is the link to the mat file format definition.
Is there a way of saving the currently running script in Matlab? I have a script which automatically takes a backup of a set of scripts although if I have changed the current script then the saved version will be out of date.
Perhaps its possible to call some java?
Thanks
Somewhere on Yair Altman's site (see link in my other answer) he also referred to a blog entry about editorservices, which was introduced with MATLAB R2009b.
editorservices.getActive().save
should do what you want.
Okay, all I write here I learned from undocumentedmatlab.com by Yair Altman, in particular by looking into his EditorMacro.m... great stuff!
I'm assuming that Itamar Katz understood you correctly and that you are running unsaved code from the editor by using "Evaluate Cell" or "Evaluate Selection"; you want your code to realize that it is not saved and save the version currently displayed in the editor to some other location.
I have not found a way to save the file directly to the original location, but at least I have found a way to access the current text. You can then use fprintf to save it to wherever you want. I have tested this in Matlab 7.11 (R2010b); if you have a different version you'd need to dig through EditorMacro.m to find the correct code for Matlab 6.
if com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.isDirty
thisdocument=com.mathworks.mlservices.MLEditorServices.getEditorApplication.getActiveEditor.getDocument;
thisdocument_text=char(thisdocument.getText(0,thisdocument.getLength));
fid = fopen('backupfile.m','w');
fprintf(fid, '%s', thisdocument_text);
fclose(fid);
else
% saved file is unmodified in editor - no need to play tricks...
...
end
So the if-condition checks if the currently active editor window contains a file which is not saved ("dirty"); if it is, we need to retrieve the current version of the code (into variable thisdocument_text) and save this string to some file.
Does this help?
I am using the Matlab Guide function in order to build an inteface with the user. Nevertheless, I need to read some arrays that are in 5 different txt files. I would like to run my guide application, and using any command, start to search my computer and load these .txt files into my program. How could I easily do it?
Thanks in advance for your help.
I don't believe that there is a built-in function that will do that for you, so either you have to loop through some dir commands yourself or grab a file from MATLAB Central / File Exchange: e.g. DIRR or subdir.