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 endend
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.matcmb_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.matsaa_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.matstt_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.matsaa_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!
Best Answer