Error in read text file

1 次查看(过去 30 天)
Mekala balaji
Mekala balaji 2016-2-9
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.

回答(1 个)

Walter Roberson
Walter Roberson 2016-2-9
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 个评论
Mekala balaji
Mekala balaji 2016-2-10
How to correct the code without any error
Walter Roberson
Walter Roberson 2016-2-10
编辑:Walter Roberson 2016-2-10
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.)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by