Error in read text file
Show older comments
I want to read text file, and extract required data. I divided into two sections, function and main. But I am getting error.
Function code (name: HCI.m):
function data=HCI(filename)
% filename='HCI.txt';
fin =fopen(filename);
for i=1:300
line{i}=fgetl(fin);
end
fclose(fin);
Str=strsplit(line{5}, ': ');
BeamSetupID=cell2mat(Str(2));
BeamSetupID=BeamSetupID(2:end);
for i=1:300
Str=strsplit(line{i}, ' ');
str(i,:)=Str(1);
end
DopTemp=regexp(line{find(strcmp(str,'Sample.Measuring time'))},' +','split');
Dop=cell2mat(DopTemp(6));
data={BeamSetupID Dop};
main code (name: main.m):
clc;
clear all;
close all;
path='D:\Mekala_Backupdata\Matlab2010\Filesfolder\PartofTextFilesData';
filetype='txt';
[files]=recursiveFileList(path,filetype);
[m n]=size(files);
outputfilenameTemp=regexp(path,'\','split');
inputfilename=['data_','_',outputfilenameTemp{5} '_' outputfilenameTemp{end} '.csv'];
Data=[];
DataFinal=[];
DataFinalTemp=[];
Var=[];
for i=1:m
filename=[path '/' files(i).name];
data=HCI(filename);
Data=[Data;data];
end
Title={'BSID' 'PPID' 'StartTime'};
DataFinalTemp=[DataFinalTemp Data];
But I am getting below error when I run:
No delimiter in string, inputString is returned ??? Index exceeds matrix dimensions.
Error in ==> HCI at 13 str(i,:)=Str(1);
Error in ==> main at 16 data=HCI(filename);
Kindl some one help.
Many thanks in advance.
Answers (1)
Walter Roberson
on 9 Feb 2016
0 votes
It is common for the last line of the file to exist but be empty or only whitespace. You do not appear to account for that possibility.
2 Comments
Mekala balaji
on 10 Feb 2016
Walter Roberson
on 10 Feb 2016
Edited: Walter Roberson
on 10 Feb 2016
Replace
for i=1:300
line{i}=fgetl(fin);
end
with
K = 0;
lines = {};
for i=1:300
thisline = fgetl(fin);
if ~ischar(thisline); break; end %end of file ?!
if ~isempty(strtrim(thisline))
K = K + 1;
lines{K} = thisline;
end
end
and replace your references to "line" with references to "lines".
Also, change
for i=1:300
to
for i = 1:length(lines)
(Please do not use "line" as the name of a variable, as line() is an important MATLAB graphics function call; there is too much risk of conflict in the meanings, and too much risk of confusion for other people reading the code.)
Categories
Find more on Standard File Formats 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!