Error to import txt data

3 views (last 30 days)
Daniel
Daniel on 27 Sep 2012
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
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
  1. nvars
  2. line1
  3. strs
  4. str
  5. i
at the stop caused by dbstop?
Daniel
Daniel on 28 Sep 2012
In this path is a sample of my dataset: https://www.dropbox.com/s/g88p61lrvo0p66p/b_ifi_1.txt?m
But, I have others 8 files with similar characteristics. They have 64 mb size and I wanna built up an only one file matlab

Sign in to comment.

Answers (1)

per isakson
per isakson on 28 Sep 2012
Edited: per isakson on 28 Sep 2012
Inspection of the file, b_ifi_1.txt, shows
  1. tab-delimited
  2. 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
Daniel
Daniel on 28 Sep 2012
I've been running the recommended function, however, the first column was deleted ... what do you think is the problem? the name of this column is "FECHA" ... Additionally, I want to build a database containing more files with exactly the same characteristics (b_ifi_2, b_ifi_3, etc. ultil b_ifi_8)... in this sense, in the function that you have suggested ¿is there the possibility of adding a loop? .. can you tell me what is the procedure to do the function?..because, Iwanna try by myself
per isakson
per isakson on 28 Sep 2012
Edited: per isakson on 28 Sep 2012
Since
  1. the information in the first column is duplicated by the information in the second and third column
  2. 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.

Sign in to comment.

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!