Open multiple indexes in file name in a loop

3 views (last 30 days)
Hi there,
I am currently trying to open a list of .txt files that differ by 2 index values. I want to be able to run a loop or loops in order to open and read each of these files for some analysis. Is there a way to call two index values for a loop that are in one file name? I know the code below that I have doesn't work but it's the closest thing that I can come up with for now.
expdata=csvread('808nm/data808avg.csv');
A=zeros(10:10);
for i=0:1
for j=0:1
sim_i_j=fopen('sims_top/808_probe_fluences%d%d.txt',i,j,'r');
sims_i_j=fscanf(sim_i_j, '%f');
for a=1:7
acc=sum(abs(log(expdata(a))-log(sims_i_j(a))));
A(i:j)=[acc];
end
end
end
find(A == min(A(:)))
Thanks for your help and suggestionsvv
  3 Comments
Chris Mcfadden
Chris Mcfadden on 24 Jun 2019
With my original code I always got an
"Error in minimum_top (line 5)
sim_i_j=fopen('sims_top/808_probe_fluences%d%d.txt',i,j,'r'); "
What you gave me should work well, although I got rid of
flist = flist(3:end)
to get a list of all of my txt files and with my txt files being in another directory,
sim_i_j = fopen(flist(i).name);
doesn't work. I moved them to be in the same directory as my script but is there a way to fix that up too?
Bob Thompson
Bob Thompson on 25 Jun 2019
Edited: Bob Thompson on 25 Jun 2019
Sorry, the reduction of flist is a hold over from just doing dir() in a directory. The first two entries are '.' and '..' respectively. Including a suffix in the call eliminates this automatically, but I forgot about that.
What do you mean 'doesn't work.' Please post the entire error message. If you are not seeing an error message, how do you know it isn't working?
Is 'sims_top/808_probe_fluences%d%d.txt' actually all the file name? I suspect that Matlab is getting confused by the /, so it is looking for a folder called 'sims_top' and then a file name called 808_probe_fluences##.txt. If that is not the entire name, and sims_top is a folder, then try reversing the / into \. I don't remember how picky Matlab is with forward and back slash for path names. Also, make sure that you have the current folder of Matlab set correctly. The fopen command you had set up originally doesn't include the full path of the files, and unless you are working in the folder which contains the files, then Matlab will not find the files you are looking for.

Sign in to comment.

Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!