How do you get the mat file name?
September 15, 2009 3:50 AM Subscribe
How do you get a Matlab script to know the name of its own .mat file?
I have a bunch of .mat files that have descriptive names (ex "round fluffy A.mat", "square fluffy B.mat") and I'd like to process the data and generate a figure with the name of the mat file, only .fig. I don't want to have to hard code the filename, or use a dialog box to input it or anything, I just want a way to be able to retrieve the argument that was used in the command that shows up when you double click the .mat file, namely
load('C:\PATH\FILENAME.mat')
I'm sure there's plenty of other ways I could be doing this, but after searching google and MATLAB help, it's kind of driving me nuts that I can't just find a simple way to do this. Am I overlooking something? Thanks.
I have a bunch of .mat files that have descriptive names (ex "round fluffy A.mat", "square fluffy B.mat") and I'd like to process the data and generate a figure with the name of the mat file, only .fig. I don't want to have to hard code the filename, or use a dialog box to input it or anything, I just want a way to be able to retrieve the argument that was used in the command that shows up when you double click the .mat file, namely
load('C:\PATH\FILENAME.mat')
I'm sure there's plenty of other ways I could be doing this, but after searching google and MATLAB help, it's kind of driving me nuts that I can't just find a simple way to do this. Am I overlooking something? Thanks.
I'm a bit confused... scripts are saved in .m files. .mat files contain saved data. As mentioned above, writing a script that loads your .mat files, you can just reuse the file name and save the figure as .fig. If you want the script to know it's own name, you could look in your 'Application Data' folder, find the matlab subfolder and look for file 'history.m'. You could extract the last line of this file in the first line of your script to get at the filename. But that's only useful if you plan on changing the filename alot... otherwise it would seem fine to hardcode it.
posted by molecicco at 5:18 AM on September 15, 2009
posted by molecicco at 5:18 AM on September 15, 2009
Probably not what he wants, but a script that wants to know its own name need only call mfilename.
posted by tss at 5:58 AM on September 15, 2009
posted by tss at 5:58 AM on September 15, 2009
This thread is closed to new comments.
dirinf = dir('*.mat');
nfiles = length(dirinf);
for n = 1:nfiles
infile = dirinf(n).name; % extract one file name from struct
outfile = infile;
outfile((end-2):end) = 'fig'; % replace last three characters
load(infile) % load the data
% do stuff to create your figure
saveas(gcf, outfile) % save the data
end
posted by swordfishtrombones at 4:10 AM on September 15, 2009