How to convert a text file into a table properly
9 views (last 30 days)
Show older comments
Hi,
I have text file that looks like this:
time-->data1-->data2-->data3-->...
0-->1.2,2.9,3.4-->4.6,5.5,3.2-->0.1,0.1,0.9-->...
0.1-->2.2,2.4,3.4-->4.8,5.8,3.8-->0.1,0.2,0.9-->...
...
Here, --> represents a tab.
I would like to create a table out of it so that the table look like this:
0 {1.2,2.9,3.4} {4.6,5.5,3.2} {0.1,0.1,0.9} ...
0.1 {2.2,2.4,3.4} {4.8,5.8,3.8} {0.1,0.2,0.9} ...
...
To do so, I first use readtable:
T_data = readtable('my_data.txt', 'Delimiter', '\t');
This will the create a table like this:
0 {'1.2,2.9,3.4'} {'4.6,5.5,3.2'} {'0.1,0.1,0.9'} ...
0.1 {'2.2,2.4,3.4'} {'4.8,5.8,3.8'} {'0.1,0.2,0.9'} ...
...
As you can see, each element of the table is a cell containing a string. Now I use a for loop with textscan to change the string cells into numeric cells:
for row = 1:height(T_data)
for col = 2:width(T_data)
T_data{row, col} = textscan(T_data{row, col}{1}, '%f', 'Delimiter', ',');
end
end
And it does the trick, however, the second part is very slow! (A 4200*8 table took around 10 seconds.)
I was wondering if there are alternative solutions that are more eficient.
Thank you in advance.
0 Comments
Answers (2)
Chunru
on 12 May 2022
T = readtable('test.txt', 'Delimiter', '-->')
for i=1:size(T,1)
for j=2:size(T,2)
T{i, j}{1} = str2num(T{i, j}{1});
end
end
T
Chunru
on 12 May 2022
Try this:
fid = fopen('test.txt', 'r');
fgetl(fid); % skip the header
i=1;
x = [];
while ~feof(fid)
s = fgetl(fid);
tmp = textscan(s, '%f', 'Delimiter', {',', '-->'});
x = [x; tmp{1}'];
end
fclose(fid);
x
0 Comments
See Also
Categories
Find more on Text Files 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!