Reputation: 19
I need to read test.TXT file(tab delimited) into MATLAB.
TXT file have form:
Datum Time Data1 Data2 Data3 Data4 Data5
06/28/2012 09:27,3 1,931764 -0,008698 4,151306 33,865424 -44,923096
06/28/2012 09:27,3 1,931764 -0,003662 4,154358 33,865424 -44,831543
06/28/2012 09:27,4 1,928712 -0,001526 4,168701 33,866102 -45,472412
.....................................................
After that I need to plot these data (for example time
vs. data1
; time
vs. data2
..)
What is the easiest way to open and read test.txt file so I can then plot my data (insert all data to workspace..). I tried to import data, but then MATLAB import data as one column. :/
Thanks
I just noticed that I copy wrong format of time into first post :S
Datum Time Data1 Data2 Data3 Data4 Data5
06/28/2012 17:09:27,3 1,931764 -0,008698 4,151306 33,865424 -44,923096
06/28/2012 17:09:27,3 1,931764 -0,003662 4,154358 33,865424 -44,831543
06/28/2012 17:09:27,4 1,928712 -0,001526 4,168701 33,866102 -45,472412
.....................................................
17:09:27,4 -> 17(hour), 09 (minutes), 27 (seconds), 4(miliseconds)
Here is now big problem with ,
and .
Upvotes: 0
Views: 7884
Reputation: 752
As answered previously in this question you should use either textscan or fscanf
After taking a closer look there are a few interesting nuances to your question. The solution I went with was to preserve the columnar structure by reading everything as a string. That helps to keep everything ordered while commas are replaced with periods, date strings are processed to datenums and finally the numbers stored as strings are converted.
fid = fopen('test.txt','rt');
header = textscan(fid,'%s %s %s %s %s %s %s',1);
data = textscan(fid,'%s %s %s %s %s %s %s');
data = cellfun(@(x) strrep(x,',','.'),data,'UniformOutput',false);
clean_data(:,1) = arrayfun(@(x) datenum([data{1}{x} ' ' data{2}{x}]), 1:length(data{1}) )';
clean_data(:,2:6) = cell2mat(cellfun(@str2num , [data{3:end}],'UniformOutput',false));
fclose(fid);
There are a few messy transitions in there. But the general approach should be solid so long as your files are not outrageously long. Here are the results:
>> datestr(clean_data(:,1))
ans =
28-Jun-2012 09:27:18
28-Jun-2012 09:27:18
28-Jun-2012 09:27:24
>> clean_data(:,2:end)
ans =
1.9318 -0.0087 4.1513 33.8654 -44.9231
1.9318 -0.0037 4.1544 33.8654 -44.8315
1.9287 -0.0015 4.1687 33.8661 -45.4724
And finally to plot versus date, which was the original goal:
>> plot(clean_data(:,1),clean_data(:,2))
>> datetick
Upvotes: 2