merging text files to manipulate the data

1 view (last 30 days)
Jamie Smith
Jamie Smith on 11 Oct 2015
Answered: Star Strider on 11 Oct 2015
I am very new to coding and so am having particular issues regarding importing text files to matlab so I can manipulate the data, i.e make graphs and tables.
There are 113 text files,(000.txt to 112.txt), each contain public information about a company in the following format
Sainsburys,18.810,78.920,99.001,5.808,41.202,56.078,18.004,75.000
the numbers represent certain characteristics. All the text files have eight numbers and the format is the same for them all.
I need to be able to create a table with all of the information together and then be able to analyse the data. My mine concern at the moment is importing all the data into one table that I can then work off.
I have used the following to open up the first text file, however doing this for all would take a long time and be inefficient.
fid = fopen('000.txt');
textData = textscan (fid, '%s%n%n%n%n%n%n%n%n',1,'Delimiter',',');
fclose(fid)
Any help would be much appreciated, thanks.

Answers (2)

Walter Roberson
Walter Roberson on 11 Oct 2015
You just need to expand "do something with the information in datacell at this point"
How you expand it will depend on what data representation you need for your table. Normal numeric arrays cannot have string stored in them. Cell arrays can have strings stored in them, but can be inefficient for use with calculations. If you have R2013b or later you could use table()

Star Strider
Star Strider on 11 Oct 2015
I don’t have your files (and I don’t want to create analogues) so I can’t test this, but it should work.
I would put your file reading in a loop, and save each file read into a cell array:
for k1 = 1:113
fid = fopen(sprintf('%03d.txt', k1-1), 'rt');
textData{k1} = textscan (fid, '%s%n%n%n%n%n%n%n%n',1,'Delimiter',',');
fclose(fid)
end
Then save your ‘textData’ cell array to a .mat file if you’re going to use it again, so you don’t have to re-read the text files.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!