Matlab: Import multiple numeric csv .txt files into single cell array -
i have multiple (say n of them) .txt files consisting of numeric csv info in matrix form. import each of these info files 1 (1 x n) cell array, whilst preserving original matrix form. if original info small, 3x3, textscan job in next manner:
fileid = fopen('data1.txt'); a{1} = textscan(fileid, '%d %d %d', 'delimiter',',','collectoutput',1); (this part of function.) if .txt files have 100 columns of data? write '%d' 100 times in formatspec, there must improve way?
this seems easy problem, i'm quite new matlab , @ loss how proceed. advice much appreciated, thanks!!
for such cases consistent info in each of text files, can utilize importdata without worrying format specifiers. 2 approaches discussed here based on it.
approach 1
filenames = {'data1.txt' 'data2.txt' 'data3.txt'}; %// cell array of filenames = cell(1,numel(filenames)); %// pre-allocation k = 1:numel(filenames) imported_data = importdata(char(filenames(k))); formatted_data = cellfun(@str2num, imported_data, 'uni', 0); a{k} = vertcat(formatted_data{:}) end approach 2
assuming text files .txt files in current working directory, can straight filenames , utilize them store info them cell array, -
files = dir('*.txt'); = cell(1,numel(files)); %// pre-allocation k = 1:numel(files) imported_data = importdata(files(k).name); formatted_data = cellfun(@str2num, imported_data, 'uni', 0) a{k} = vertcat(formatted_data{:}) end matlab csv cell-array textscan
No comments:
Post a Comment