While trying to write in a.txt or .csv file, I am receiving the message "Index exceeds the number of array elements (0)"

조회 수: 1 (최근 30일)
I am trying to wite my output into a .csv file and a .txt file after scaning the attached .txt file (imported_PEVT.txt). However, I am getting the following error " Index exceeds the number of array elements (0)". Could anybody please help me in this regard? Thanks.
function main
fid = fopen('imported_PEVT.txt', 'r');
y = [];
m = [];
d = [];
h = [];
mm = [];
v = [];
while feof(fid) == 0
tline = fgetl(fid);
out = sscanf(tline, '%d %d %d %d %d %f');
y1 = out(1);
m1 = out(2);
d1 = out(3);
h1 = out(4);
mm1 = out(5);
v1 = out(6);
y = [y; y1];
m = [m; m1];
d = [d; d1];
h = [h; h1];
mm = [mm; mm1];
v = [v; v1];
end
write_output_file_pevt_notime('pevt_no_t.csv', v);
write_output_file_pevt_wtime('pevt_w_t.txt', y, m, d, h, mm, v);
end
function write_output_file_pevt_notime(fname, v)
fid = fopen(fname, 'w');
fprintf('%f\n',[v]');
fprintf(fid,'%f\n',[v]');
fclose(fid);
end
function write_output_file_pevt_wtime(fname, y, m, d, h, mm, v)
fid = fopen(fname, 'w');
fprintf('%d %d %d %d %d %f\n',[y m d h mm v]');
fprintf(fid,'%d %d %d %d %d %f\n',[y m d h mm v]');
fclose(fid);
end
  댓글 수: 2
bharath pro
bharath pro 2020년 6월 30일
The above code is working fine for me. May I know which version of matlab you are using?
Preyanka Dey
Preyanka Dey 2020년 6월 30일
Hi @bharath pro, Error was because of the a empty line in my .txt file. @SC pointed out that. Thanks both of you a lot. I am using Mathlab 2019.

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

채택된 답변

SC
SC 2020년 6월 30일
The last line in your text file (line:78889) is an empty line
clear that out & your code works!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by