reading a txt file including dash

6 views (last 30 days)
Hello,
I have a .txt file and want to read it in Matlab (actually to read the last raw) but I get the following error:
Error using cat Dimensions of matrices being concatenated are not consistent._
Error in cell2mat (line 75) m{n} = cat(2,c{n,:});
Error in STF_v0 (line 52)
final_data = cell2mat(textscan(fileID ,tkn,'delimiter', '\t', 'MultipleDelimsAsOne',true));
I imagine that is due to -- I have in the file. Any idea how I can fix this? The txt file looks like this and I have also attached it to the message.
Thank you
Below is the code I am using.
fileID = fopen(fullFileName);
column_headers = regexp(fgetl(fileID ),'\t+','split');
tkn = repmat('%f',1,numel(column_headers));
final_data = cell2mat(textscan(fileID ,tkn,'delimiter', '\t', 'MultipleDelimsAsOne',true));
fclose(fileID );

Accepted Answer

per isakson
per isakson on 26 Jul 2017
One way
fid = fopen( 'DI_WATER.txt' );
cac = textscan( fid, '%f%f%f%f%f%f%f%f' ...
, 'Headerlines',1, 'Delimiter','\t' ...
, 'TreatAsEmpty','--', 'CollectOutput', true );
fclose( fid );
num = cac{1};
inspect result
>> whos num
Name Size Bytes Class Attributes
num 20x8 1280 double
>> num(end,:)
ans =
20.0000 61.0000 2.8975 0.2955 72.0764 72.0830 0.0056 24.5100
>>

More Answers (0)

Community Treasure Hunt

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

Start Hunting!