Error in read text file
1 回表示 (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
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 件のコメント
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 Center および File Exchange で Entering Commands についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!