How to import data from text file to mat file directly with lebels
Show older comments
I have data in txt file like:
Date Hour Degree Speed
20110101 01 260 41
20110101 02 280 62
20110101 03 270 62
20110101 04 270 52
.
.
.
.
.
I want to have a mat file with
Answers (2)
Andrei Bobrov
on 4 Mar 2013
Edited: Andrei Bobrov
on 4 Mar 2013
f = fopen('yourfile.txt');
c = textscan(f,'%s');
fclose(f);
C = reshape(c{:},4,[])';
out2 = cell2struct([{C(2:end,1)} num2cell(str2double(C(2:end,2:end)),1)],C(1,:),2);
Jan
on 4 Mar 2013
Or:
Str = fileread('yourfile.txt');
[First, Rest] = strtok(Str, char(10));
Title = regexp(First, ' ', 'split'); % Or a TAB?
Title = strrep(Title, ' ', ''); % Remove spaces
Data = sscanf(Rest, '%g', [Inf, 5]);
for iField = 1:5
Out.(Title{iField}) = Data(:, iField);
end
save('YourMatFile.mat', 'Out'); % Or '-struct', 'Out' ?
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!