Error to import txt data
显示 更早的评论
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 个评论
per isakson
2012-9-27
What's in b_ifi_1.txt? Does it match the default values of dataset?
Daniel
2012-9-27
per isakson
2012-9-27
编辑:per isakson
2012-9-27
I don't think the size is the problem.
Try
dbstop if error
and check what values the variables, strs and i, hold at the line, which cause the error
Daniel
2012-9-27
per isakson
2012-9-27
I don't understand your question.
Did you read the page, "dataset, Class: dataset, Construct dataset array", carefully?
Could you show a few columns of the first few lines?
Tom
2012-9-27
Maybe strs, or strs{i} for one case is empty.
per isakson
2012-9-27
编辑:per isakson
2012-9-27
@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?
Daniel
2012-9-28
回答(1 个)
per isakson
2012-9-28
编辑:per isakson
2012-9-28
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 个评论
Daniel
2012-9-28
per isakson
2012-9-28
编辑:per isakson
2012-9-28
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Large Files and Big Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!