1

This is my code

function1( 'myfile.20140826')
plot( 'myfile.20140826.shd.mat', 1, 1, 1 )
saveas(gcf(), 'myfile.20140826.20140826.pdf') 

function1( 'myfile2.20140826' )
plot( 'myfile2.20140826.shd.mat', 1, 1, 1 )
saveas(gcf(), 'myfile2.20140826.pdf') 

I have several files that I would like it to run and plot automatic. This file have almost the same name, but the date in the name changes (myfile.20140826, myfile.20160827 ect.)Any ideas? Maybe make a for loop or something?

Mfj
  • 87
  • 1
  • 6
  • 1
    You can get all the file names with `dir`, and use the result to loop over the files. – Adiel Sep 06 '17 at 11:52

2 Answers2

2

Save the files you want to read in your directory and run:

Files=dir(fullfile(pwd,'\*.shd.mat'));
for j=1:length(Files)
 str=strsplit(Files(j).name,'.');
 res=sprintf('%s.%s',str{1},str{2});
 function1(res);
 plot(Files(j).name, 1, 1, 1 );
 saveas(gcf(), strcat(res,'.pdf'));
end

The command dir(fullfile(pwd,'\*.shd.mat')) will read all the files you have in your current folder.

hello123
  • 313
  • 1
  • 7
  • 1
    On Linux you need the back-slash- `/`. But actually, on both OS you don't need it at all. `dir(fullfile(pwd,'*.shd.mat'))` will work. – Adiel Sep 06 '17 at 13:28
1

To elaborate on Adiel's comment:

Files=dir('myfile*') %takes wildcards

for ii=1:length(Files)

   function1( Files(ii).name)
   % ...

end
McMa
  • 1,568
  • 7
  • 22