Loading ASCII tables into MATLAB as strings to be processed
2 views (last 30 days)
Show older comments
I am working on a general script to automate plotting of selective columns and rows from large ASCII data sets.
My issue is I am having extreme difficulty loading the data into MATLAB in a way that would allow for searching through the data set. For example, an example data set may look like
- 1055+018 1 2001 Oct 19 5 6 7
- 1055+018 2 2002 Nov 21 8 9 10
- 1055+018 2 2002 Dec 12 11 12 13
- 5055+018 1 2000 Jan 10 14 15 16
- 5055+018 1 2001 Feb 11 17 18 19
- 5055+018 2 2002 Mar 12 20 21 22
I am attempting to write the code in such a way that the use could enter which item from column 1 they would want, and then the necessary rows and columns based off information found in the second row; however, due to the format of the ASCII data sets I cannot figure out a way to load data correct, even as strings, to read the whole dataset. Usually, it will just be unable to load due to the alphabetical months or cut off information depending on which loading feature I am using.
Does anyone have any suggestions on what to do?
0 Comments
Accepted Answer
per isakson
on 20 Jul 2012
Edited: per isakson
on 20 Jul 2012
I assume that your data is in a text file. This code will read the file
fid = fopen( 'cssm.txt' );
cac = textscan( fid, '%d%d%d%d%s%d%d%d%d' );
fclose( fid )
where cssm.txt contains
1055+018 1 2001 Oct 19 5 6 7
1055+018 2 2002 Nov 21 8 9 10
...
There is a result in cac
>> cac =
Columns 1 through 7
[6x1 int32] [6x1 int32] [6x1 int32] [6x1 int32] {6x1 cell} [6x1 int32] [6x1 int32]
Columns 8 through 9
[6x1 int32] [6x1 int32]
0 Comments
More Answers (3)
Star Strider
on 20 Jul 2012
Edited: Star Strider
on 20 Jul 2012
This worked for me when I copy-pasted your data to a test routine:
Data = {'1055+018 1 2001 Oct 19 5 6 7'
'1055+018 2 2002 Nov 21 8 9 10'
'1055+018 2 2002 Dec 12 11 12 13'
'5055+018 1 2000 Jan 10 14 15 16'
'5055+018 1 2001 Feb 11 17 18 19'
'5055+018 2 2002 Mar 12 20 21 22'};
for k1 = 1:size(Data,1)
InputCell(k1,:) = textscan( Data{k1,:}, '%d%d %d %d %3c %d %d %d %d')
end
I got back:
InputCell =
[1055] [18] [1] [2001] 'Oct' [19] [ 5] [ 6] [ 7]
[1055] [18] [2] [2002] 'Nov' [21] [ 8] [ 9] [10]
[1055] [18] [2] [2002] 'Dec' [12] [11] [12] [13]
[5055] [18] [1] [2000] 'Jan' [10] [14] [15] [16]
[5055] [18] [1] [2001] 'Feb' [11] [17] [18] [19]
[5055] [18] [2] [2002] 'Mar' [12] [20] [21] [22]
Is that the sort of result you want? I don't use ‘textscan’ that much so others may have better solutions, but this might suggest a way for you to at least read your files. I refer you to the ‘textscan’ documentation for details.
0 Comments
See Also
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!