Trying to pass a variable name for a file in a for loop to xlsread

4 views (last 30 days)
I'm trying to use xlsread to open a variable filename on each iteration of a for loop. The code I initially used was
for vj=1:prime_file_num
BaseName='15_File_';
k=prime_file_num1(vj,1);
FileName=[BaseName,num2str(k)];
[~,~,rawS] = xlsread(FileName);
which worked for one iteration, then just seemed to grind away forever trying to read the next File, and now just grinds even on the first iteration. I tried changing the code to
for vj=1:prime_file_num
BaseName='15_File_';
k=prime_file_num1(vj,1);
FileName=[BaseName,num2str(k)];
pathname = 'C:\Documents\MATLAB\FileName.xlsx';
[~,~,rawS] = xlsread(pathname);
But that just returns the error that xlsread can't find the file--its trying to open a file named 'C:\Documents\MATLAB\FileName.xlsx, rather than '15_File_2' for example.
Can anybody tell me what I'm doing wrong or is what I'm trying even possible. Thanks
  4 Comments
Mark Bodner
Mark Bodner on 26 Sep 2021
Also tried the code and its still just trying to open a file calle 'C:\Documents\MATLAB\FileName.xlsx'. Its not treating it as a path but rather as the file name itself. So there's obviously something missing?
Stephen23
Stephen23 on 27 Sep 2021
Edited: Stephen23 on 27 Sep 2021
"so does this still work?"
Yes. Look at the code: it uses prime_file_num1 to define the values used in the filenames.
"So there's obviously something missing?"
Make sure that pathname does not include "Filename.xlsx" : check my previous comment again!

Sign in to comment.

Accepted Answer

Jan
Jan on 26 Sep 2021
Edited: Jan on 27 Sep 2021
pathname = 'C:\Documents\MATLAB\FileName.xlsx';
This is a fixed CHAR vector and does not consider the variable FileName.
Stephen's appraoch is working, if the path name is fixed:
BaseName = '15_File_%d.xlsx'; % [EDITED, file extension added]
pathname = 'C:\Documents\MATLAB\';
for vj = 1:prime_file_num
filename = sprintf(BaseName, prime_file_num1(vj, 1));
file = fullfile(pathname, filename);
% [~,~,rawS] = xlsread(file);
disp(file); % For testing
...
end
  2 Comments
Mark Bodner
Mark Bodner on 26 Sep 2021
For whatever reason, that doesn't work! It just keeps trying to open a file called 'C:\Documents\MATLAB\15_File_2'. It doesn't see anything as a pathway, but rather as the name of the file trying to be read, and then returns the error that the file is not found. I don't know what is going wrong.
Jan
Jan on 27 Sep 2021
Edited: Jan on 27 Sep 2021
No, my code does not open anything. It displays the file names only. This means, that you are doing something else and I cannot know, what it is.
Solve the problem step by step. Start with the shown code. Does it work? Do you see the different file names? If all filenames are equal, you have shown, that prime_file_num1 contains only the value 2.
Please post the output of the above code, if you get any problems there.
If this is working proceed with the next step to uncomment the xlsread() command. Do you get an error message now? If so, please post a copy of the complete message.

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!