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일

0 개 추천

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.)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Standard File Formats에 대해 자세히 알아보기

태그

질문:

2016년 2월 9일

편집:

2016년 2월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by