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.
posted by cali59 to computers & internet (3 comments total)
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