MATLAB: Import and sorting text file to matlab

data importsortstructuretext file

hi all.
I have these hundreds text files with some data i should analyze
each file has date column time column, ID column and value column.
i would like to create a structure that contains text file name and ID as fields and then fill it with the respectvie times and values. i wpould like to create a general code that can be used in every future situation, because i'm going to get more and more of this text files to analyze.

Best Answer

  • Import all the text files
folder = 'c:\somewhere\somefolder';
filelist = {'fileA', 'fileB', ...}; %obtained however you want. e.g with dir
alldata = cell(size(filelist));
for fileidx = 1:numel(filelist)
%load file
filedata = readtable(fullfile(folder, filelist{fileidx});
%optionally, tidy up the table:
filedata = mergevars(filedata, 1:3);
filedata.Properties.VariableNames = {'Date', 'Time', 'ID', 'something'};
filedata.Date = datetime(filedata.Date);
%store in cell array
alldata{fileidx} = filedata;
end
%once everything is loaded, merge it all in one table
alldata = vertcat(alldata{:});
%sort by time
alldata = sortrows(alldata, 'Time');
  • put in a structure. Not needed if all you want to plot per ID
  • plot each ID in one graph (I assume it's column 4 (which I called something) against time).
%create figure, axes, and tell matlab to plot all on the same graph
figure;
axes;
hold on;
%plot something vs time for each unique ID.
rowfun(@plot, alldata, 'GroupingVariables', 'ID', 'InputVariables', {'Time', 'something'});