Bizarre issue: old file list recalled after using load()

2 views (last 30 days)
Hi folks,
So I have some really simple code to load MATLAB files one by one, and write some data pulled from those files into a corresponding pre-existing file in Excel. I'm utterly flummoxed by what's happening in my code. Here is my code:
if true
fileList = ls('*exp.mat')
[m n] = size(fileList)
for i = 1:m
fileName = deblank(fileList(i,:));
excelName = fileList(i,1:3);
excelFile = strcat('F:\PhD\Pilot Studies\Reaching and Grasping\Excel Spreadsheets\', upper(excelName), '.xlsx');
a = strfind(fileName, 'dom'); % find out what type of file it is
b = strfind(fileName, 'nondom');
c = strfind(fileName, 'binoc');
if isempty(c) == 0; % if filename is binocular
fileType = 1; % use filetype 1
end
if isempty(a) == 0; % if filename is for dominant eye
fileType = 2; % use filetype 2
end
if isempty(b) == 0; % if filename is for nondominant eye
fileType = 3; % use filetype 3
end
load(fileName);
tempfile = [data(:,[1 2 3 7])]; % get the data we want
if fileType == 1; % set target cell according to file type
targetCell = 'D2:G59';
end
if fileType == 2;
targetCell = 'D64:G121';
end
if fileType == 3;
targetCell = 'D126:G183';
end
% initiate xlswrite1 code
Excel = actxserver ('Excel.Application');
File=excelFile;
if ~exist(File,'file')
ExcelWorkbook = Excel.workbooks.Add;
ExcelWorkbook.SaveAs(File,1);
ExcelWorkbook.Close(false);
end
invoke(Excel.Workbooks,'Open',File);
xlswrite1(excelFile, tempfile, 1, targetCell); % write the MATLAB data to the specified cell in the spreadsheet
% close the Excel active server
invoke(Excel.ActiveWorkbook,'Save');
Excel.Quit
Excel.delete
clear Excel
end
end
Works fine as an individual code block outside of loop, but I put it in a loop because I might be using it again in the future for other files. Soooo. The contents of my fileList variable are supposed to be as follows:
if true
ajs_141113_binoc_block2_exp.mat
ajs_141113_domleft_block1_exp.mat
ajs_141113_nondomright_block3_exp.mat
cmb_080713_binoc_block1_exp.mat
cmb_080713_domright_block2_exp.mat
cmb_080713_nondomleft_block3_exp.mat
eom_081113_binoc_block3_exp.mat
eom_121113_domright_block2_exp.mat
eom_121113_nondomleft_block1_exp.mat
mep_040613_binoc_exp.mat
mep_040613_domleft_exp.mat
mep_040613_nomdomright_exp.mat
mrj_270613_binoc_block2_exp.mat
mrj_270613_domright_block3_exp.mat
mrj_270613_nondomleft_block1_exp.mat
noj_040713_binoc_block2_exp.mat
noj_040713_domleft_block1_exp.mat
noj_040713_nondomright_block3_exp.mat
saa_111113_binoc_block2_exp.mat
saa_111113_domright_block3_exp.mat
saa_111113_nondomleft_block1_exp.mat
sjs_131113_binoc_block3_exp.mat
sjs_131113_domleft_block2_exp.mat
sjs_131113_nondomright_block1_exp.mat
stt_260913_binoc_block1_exp.mat
stt_260913_domright_block3_exp.mat
stt_260913_nondomleft_block2_exp.mat
tec_040713_binoc_block3_exp.mat
tec_040713_domright_block1_exp.mat
tec_040713_nondomleft_block2_exp.mat
end
If you step through my code, it stays like that right up until executing load(fileName) - after executing that, the fileList changes to...
if true
TEC_040713_domright_block1_exp.mat
air_260613_binoc_block3_exp.mat
air_260613_domright_exp.mat
air_260613_nondomleft_block1_exp.mat
as_141113_binoc_block2_exp.mat
as_141113_domleft_block1_exp.mat
as_141113_nondomright_block3_exp.mat
cmb_080713_binoc_block1_exp.mat
cmb_080713_domright_block2_exp.mat
cmb_080713_nondomleft_block3_exp.mat
em_081113_binoc_block3_exp.mat
em_121113_domright_block2_exp.mat
em_121113_nondomleft_block1_exp.mat
mefp_040613_bin_exp.mat
mefp_040613_mon_l_exp.mat
mefp_040613_mon_r_exp.mat
mrj_270613_binoc_block2_exp.mat
mrj_270613_domright_block3_exp.mat
mrj_270613_nondomleft_block1_exp.mat
noj_040713_binoc_block2_exp.mat
noj_040713_domleft_block1_exp.mat
noj_040713_nondomright_block3_exp.mat
saa_111113_binoc_block2_exp.mat
saa_111113_domright_block3_exp.mat
saa_111113_nondomleft_block1_exp.mat
ss_131113_binoc_block3_exp.mat
ss_131113_domleft_block2_exp.mat
ss_131113_nondomright_block1_exp.mat
stt_260913_binoc_block1_exp.mat
stt_260913_domright_block3_exp.mat
stt_260913_nondomleft_block2_exp.mat
tec_040713_binoc_block3_exp.mat
tec_040713_nondomleft_block2_exp.mat
end
See the capitalised TEC at the top? And that extra subject with initials air? And how the subject initials are sometimes only two letters long? I changed all of these things a day ago. So why is my use of the load function calling up an obsolete list of files that last existed a day ago, when I haven't even called ls as part of the load function, never mind assigning it the variable fileList? It also messes up my code loop as on the 2nd iteration it's trying to load a file that no longer exists, due to using this obsolete file list. If I run fileName = ls('*exp.mat') again, the proper file list comes up, that doesn't have the extra subject and the capitalised TEC.
I mean, it's not a big deal, I can just move the generation of fileList inside the loop and remake the list every time. I'm just really bemused as to why calling load() not only alters my other completely unrelated variable, but also uses a legacy list of files from a day ago that doesn't exist when you call ls() now. I used inmem to see what MATLAB was holding in memory and cleared ls as a result but it made no difference. Does anyone have any explanation?
Thanks!
  2 Comments
Walter Roberson
Walter Roberson on 24 Jan 2014
Make sure that you have closed all files before you begin.
If you are writing to .m files that are in use, then you need to "clear" the function name before the changed version becomes usable in that MATLAB session.
Marianne
Marianne on 10 Apr 2014
Edited: Marianne on 10 Apr 2014
As an update, I encountered this problem again with another piece of code doing a similar kind of activity. I again had to move the list generation inside the loop, otherwise it substituted a random filelist from a completely different folder! I also got this error when using the line of code calling the load() function: Warning: Interface.000208DA_0000_0000_C000_000000000046 object could not be loaded.

Sign in to comment.

Accepted Answer

Jan
Jan on 10 Apr 2014
Edited: Jan on 10 Apr 2014
Calling load() without catching the output has such side-effects, when a variable stored inside the MAT-file overwrites a locally used variable from the code:
a = 1;
data = rand(5);
save('test.mat');
a = 17;
load('test.mat');
disp(a) % 1 !!!
Therefore it is a good programming pattern to catch the output:
FileData = load(fileName);
tempfile = [FileData.data(:,[1 2 3 7])];
Using ls('*exp.mat') to get a list of file names is very indirect. because it converts the output of dir to a CHAR-Matrix padded with spaces. Better:
fileDir = dir(*exp.mat');
fileName = {fileDir.name};

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!