Error to import txt data
3 views (last 30 days)
Show older comments
I want to import txt file (b_ifi_1.txt) to Matlab; my code is:
data=dataset('File','b_ifi_1.txt')
however, i have this message
Index exceeds matrix dimensions.
Error in dataset/readFile (line 123) str = strs{i}{1};
I don´t know if this dataset is so large or I have a problem about my memory o capacity configuration´
Hel me please
8 Comments
per isakson
on 27 Sep 2012
Edited: per isakson
on 27 Sep 2012
@Daniel, don't rely on my guesses! Show a a few columns of the first few lines of the text file!
What are the values of
- nvars
- line1
- strs
- str
- i
at the stop caused by dbstop?
Answers (1)
per isakson
on 28 Sep 2012
Edited: per isakson
on 28 Sep 2012
Inspection of the file, b_ifi_1.txt, shows
- tab-delimited
- plenty of "missing data"
The error you report is most likely due to "missing data" in the first row of data. DATASET uses the first data row to figure out the format. The error occurs in that part of that code.
.
--- Working code ---
Try
>> data = cssm();
>> whos data
Name Size Bytes Class Attributes
data 155x1212 1809384 dataset
where cssm.m
function dat = cssm()
file_spec = 'h:\m\cssm\b_ifi_1.txt';
%%Read the rows as strings
fid = fopen( file_spec );
cac = textscan( fid, '%s', 'BufSize', 2e4, 'Delimiter', '\n' );
sts = fclose( fid ); %#ok<*NASGU>
cac = cac{1};
%%Assert that all rows have the same number of columns
num_tab = cellfun( @(str) numel( strfind( str, char(9) ) ), cac );
assert( all( diff( num_tab ) == 0 ) ...
, 'cssm:IsRagged', 'Varying number of tabs' )
%%Read the file to a string variable and strip of the first line
str = fileread( file_spec );
ix1 = find( str==char(10), 1 , 'first' );
str = str(ix1+1:end);
%%Test parsing the string with textscan
frm = '%*s%u%u%u%u%q%u%u';
frm = cat( 2, frm, repmat( '%f', [ 1, median(num_tab) - 8 + 1 ] ) );
buf = textscan( str, frm, 'BufSize',2e4, 'Delimiter','\t' );
%%Import the file to a dataset
dat = dataset( 'File', file_spec ...
, 'Format',frm, 'BufSize',2e4, 'Delimiter','\t' );
end
and where 'h:\m\cssm\b_ifi_1.txt' is the file from dropbox
2 Comments
per isakson
on 28 Sep 2012
Edited: per isakson
on 28 Sep 2012
Since
- the information in the first column is duplicated by the information in the second and third column
- Matlab cannot use the names in the first column
I suppressed reading the first column by adding a "*" to the format string '%*s'.
You need to understand the code, which you get from a forum like this. I don't test that carefully. The function, cssm, is intended as help. You should make your own version to use in real work. You need to add some error handling for one.
Try to make something, "loop over all files", and return here if you run into problems.
See Also
Categories
Find more on Cell Arrays 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!