Matlab - why doesn't fileID variable update when importing data using for loop? -


i'm using matlab's import data code generator pass data series of commands. works fine when run script , reference single file, if loop through several files, variables aren't updated expect. believe have traced problem 'fileid' not updating after first iteration of loop.

in code below, can confirm 'filename' updated each iteration of loop, while 'fileid' not. consequently, same vector assigned variable 'y' in each iteration.

can suggest going wrong?

filelist = dir('*.csv'); n = size(filelist,1); k = 1:n   % file name: filename = filelist(k).name; delimiter = ','; startrow = 2;  %% format string each line of text: %   column2: double (%f) %   column3: double (%f) %   column4: double (%f) %   column5: double (%f) % more information, see textscan documentation. formatspec = '%*s%f%f%f%f%[^\n\r]';  %% open text file. fileid = fopen(filename,'r');  %% read columns of data according format string. dataarray = textscan(fileid, formatspec, 'delimiter', delimiter, 'headerlines' ,startrow-1, 'returnonerror', false);  %% close text file. fclose(fileid);  %% allocate imported array column variable names o1 = dataarray{:, 1}; h1 = dataarray{:, 2}; l1 = dataarray{:, 3}; c1 = dataarray{:, 4};  %% test filename , fileid filename fileid  %% clear temporary variables clearvars filename delimiter startrow formatspec fileid dataarray ans; y=c1; figure  plot(y);  end 

fileid not supposed change expect. fileid file identifier, extracted data in dataarray scan text of fileid.

so fileid equal 3 if open file , closed before open new one. if don't close there different number in fileid each file.


Popular posts from this blog