From: Morgan Fox on
I am processing some data, and I need to save a plot with the same name as the file from which I loaded the data.

I have the files filename-01-01-09.txt, filename-01-02-09.txt, up to filename-04-05-10.txt (dates), ie not numbered filename1.txt, filename2.txt.

So I can't use something like this:
% Read files file1.txt through file20.txt, mat1.mat through mat20.mat
% and image1.jpg through image20.jpg
for k = 1:20
matfilename = sprintf('mat%d.mat', k);
matdata = load(matfilename);
jpgfilename = strcat('image', num2str(k), '.txt');
imagedata = imread(jpgfilename);
textfilename = ['file' num2str(k) '.txt'];
fid = fopen(textfilename, 'rt');
textdata = fread(fid);
fclose(fid);
end
__
%Say I want to look at filename-09-21-09.txt:

data=load(filename-09-21-09.txt); %now I have my data vector

% I do some analysis, and then want to plot the results

plot(x,y)

% Now here is the problem. I want to save this plot as filename-09-21-09.jpg. How do I call the filename up in saveas()? Or should I do this another way?

Thanks.
From: Walter Roberson on
Morgan Fox wrote:

> %Say I want to look at filename-09-21-09.txt:
>
> data=load(filename-09-21-09.txt); %now I have my data vector
> % I do some analysis, and then want to plot the results
>
> plot(x,y)
>
> % Now here is the problem. I want to save this plot as
> filename-09-21-09.jpg. How do I call the filename up in saveas()? Or
> should I do this another way?


basename = 'filename-09-21-09';
inputname = [basename '.txt'];
data = load(inputname);
.....
plot(x,y);
imagename = [basename '.jpg'];
saveas(gcf, imagename, 'jpg');
From: Morgan Fox on
Walter Roberson <roberson(a)hushmail.com> wrote in message <i2kndr$95m$1(a)canopus.cc.umanitoba.ca>...
> Morgan Fox wrote:
>
> > %Say I want to look at filename-09-21-09.txt:
> >
> > data=load(filename-09-21-09.txt); %now I have my data vector
> > % I do some analysis, and then want to plot the results
> >
> > plot(x,y)
> >
> > % Now here is the problem. I want to save this plot as
> > filename-09-21-09.jpg. How do I call the filename up in saveas()? Or
> > should I do this another way?
>
>
> basename = 'filename-09-21-09';
> inputname = [basename '.txt'];
> data = load(inputname);
> ....
> plot(x,y);
> imagename = [basename '.jpg'];
> saveas(gcf, imagename, 'jpg');


A problem is that I am calling files from a directory by

num=10 % the 10th file in the folder. I change this each time to select a file to analyze/plot
folder=dir('*out.txt'); % lists all -out data
file=folder(num).name %gives file a name
data=load(file)
__

the problem is that I can't say basename='filename-09-21-09' because I would have to enter the dates individually and that's what I'm trying to get around. Using basename='file' doesn't work because it takes the string literally. Any ideas about how to get around this?

Thanks for your help.
From: Walter Roberson on
Morgan Fox wrote:

> A problem is that I am calling files from a directory by
> num=10 % the 10th file in the folder. I change this each time to select
> a file to analyze/plot
> folder=dir('*out.txt'); % lists all -out data
> file=folder(num).name %gives file a name
> data=load(file)
> __
>
> the problem is that I can't say basename='filename-09-21-09' because I
> would have to enter the dates individually and that's what I'm trying to
> get around. Using basename='file' doesn't work because it takes the
> string literally. Any ideas about how to get around this?

[PATHSTR,NAME,EXT,VERSN] = fileparts(file);
imagefilename = fullfile(PATHSTR, [NAME '.jpg' VERSN]);