Loading data to vector rather than cell in MATlab -
i load data text files matlab function using code:
data = cell(h.numdirs, numdatafilesinfirstdir); d = 1:h.numdirs % code set filenames, idir t = 1:size(filenames,1) fid = fopen([idir, '/', filenames{t}]); % drop first 2 lines (column headers) skip = 1:2 fgets(fid); end u_temp = fscanf(fid, '%f %f', [2, inf]); u_temp = u_temp'; % ' transpose (syntax highlighting on so) data(d, t) = {u_temp(:,2)}; fclose(fid); end end
the files should each have same length (at least varying t
, varying d
or else have problems later)
should (/ how can i) simplify code here avoid (unnecessary?) cells?
i scan first data set, use
data = zeros(h.numdirs, numdatafilesinfirstdir, lengthoffirstfile)
but don't know if better. 'better' solution/method?
i use dlmread
instead of fscanf
. data type hard since dimensions vary. wouldn't pad arrays... benefit not using cells overcome complexity , memory hit. cell arrays reasonable choice. wouldn't worry preallocation in case actually. below similar option using structs
dynamic field names embed source directory , filename, later reference.
data = struct(); d = 1: ... t = 1: ... file = fullfile(idir, filenames{t}); range = [3, 1, inf, 2]; dlm = ' '; utemp = dlmread(file, dlm, range); data.(idir).(filenames{t}) = utemp(:, 2);
Comments
Post a Comment