How to structure input data as what I need?
3 views (last 30 days)
Show older comments
I have a text file with content like this:
35.921 M, 220,000, 48.509 M, 6.349 M, 54.052 M, 806,362, 41.018 M, 4.006 M, 41.564 M, 3.46 M, 40.02 M, 100,000, 39.22 M, 900,000, 36.974 M, 30,000, 36.549 M, 455,500, 35.739 M, 230,000, 33.104 M, 2.866 M, 19.627 M, 0, 18.877 M, 750,000, 19.603 M, 4.379 M, 23.982 M, 0, 10.186 M, 250,000, 10.436 M, 0, 15.414 M, 250,500, 15.465 M, 200,000, 21.571 M, 665,000, 22.236 M, 0, 15.537 M, 250,000, 15.787 M, 0, 21.422 M, 221,004, 21.243 M, 400,000, 30.662 M, 2.375 M, 33.036 M, 0, 39.287 M, 455,000, 39.742 M, 0, 53.141 M, 6.11 M, 59.131 M, 120,000, 23.587 M, 255,000, 23.842 M, 0, 17.043 M, 255,000, 17.298 M, 0, 33.51 M, 1.25 M, 34.75 M, 10,000, 36.408 M, 15,000, 28.248 M, 8.175 M, 32.367 M, 480,000, 31.535 M, 1.312 M, 54.773 M, 2.68 M, 43.936 M, 13.517 M, 58.955 M, 2.54 M, 41.234 M, 20.262 M, 45.222 M, 15,000
That I like to fill them into a N*4 dimentional matrix like this:
35.921 M, 220,000, 48.509 M, 6.349 M
54.052 M, 806,362, 41.018 M, 4.006 M
41.564 M, 3.46 M, 40.02 M, 100,000
How can I do it on Matlab?
I have attached my file to my question.
2 Comments
Accepted Answer
Stephen23
on 20 Jan 2019
Edited: Stephen23
on 21 Jan 2019
str = fileread('store.txt');
str = strtrim(str);
C = regexp(str,'\s*,\s*','split');
[fid,msg] = fopen('store2.txt','wt');
assert(fid>=3,msg)
fprintf(fid,'%s, %s, %s, %s, %s\n', C{:});
fclose(fid);
EDIT: see the comments for further developments of this code.
8 Comments
Stephen23
on 21 Jan 2019
"Can I do web scrapping with Matlab?"
An internet search brings up plenty of pages to read:
More Answers (0)
See Also
Categories
Find more on Characters and Strings 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!