I want to read a data pattern from a txt file and concatenate each line together. I wrote the following program but the error message shows:
function read_datapattern(filename)
fid = fopen(filename,'rt');
if fid < 0
error('error opening file %s\n\n',filename);
end
pattern = fgets(fid);
for n = 1:32767
nextline = fgets(fid);
pattern = strcat(pattern, nextline);
fprintf(pattern);
fprintf('\n');
fclose(fid);
end
"Invalid file identifier. Use fopen to generate a valid file identifier." "nextline = fgets(fid);"
How do I need to modify the program? Thank you~

 採用された回答

KSSV
KSSV 2016 年 9 月 29 日
編集済み: KSSV 2016 年 9 月 29 日

0 投票

You are closing the file in the loop. close the file after the loop. I assume the below should work.
function read_datapattern(filename)
fid = fopen(filename,'rt');
if fid < 0
error('error opening file %s\n\n',filename);
end
pattern = fgets(fid);
for n = 1:32767
nextline = fgets(fid);
pattern = strcat(pattern, nextline);
fprintf(pattern);
fprintf('\n');
end
fclose(fid);

1 件のコメント

Zhonghao Liao
Zhonghao Liao 2016 年 9 月 29 日
Yes, that is work. Thank you~

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

質問済み:

2016 年 9 月 29 日

コメント済み:

2016 年 9 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by