fopen issues, not writing properly
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi everyone,
basename = 'cuboid';
ending = strcat('_',num2str(a),'.txt');
name = strcat(basename,ending);
fileID = fopen(name,'w');
fprintf(fileID,'%22.16f %22.16f %22.16f %22.16f %22.16f %22.16f %22.16f \r\n',transpose(grid));
fclose(fileID);
fileID = [];
fprintf('Finished %d \n',a)
I am trying to write data from a matrix to a text file called cuboid_# where pound is a number given by a for loop index, as I am trying to write many. It works for the first few hundred, but around a = 470, the script fails with the following error:
"Error using fclose Invalid file identifier. Use fopen to generate a valid file identifier.
Error in cuboid_timescan (line 56) fclose(fileID); "
The file ID is consistently positive, so I am not sure what the issue is, especially since it works for files of a lower index.
Does anyone know what the issue here is?
댓글 수: 0
채택된 답변
Image Analyst
2018년 7월 20일
Lots of things wrong with your code. Just try this. Ask questions if you don't understand it.
baseName = 'cuboid';
a = 123;
grid1 = rand(1, 7); % Don't use grid as a name!!!!
baseFileName = sprintf('%s_%d.txt', baseName, a)
fullFileName = fullfile(pwd, baseFileName)
fileID = fopen(fullFileName, 'wt');
fprintf(fileID,'%22.16f %22.16f %22.16f %22.16f %22.16f %22.16f %22.16f \r\n',transpose(grid1));
fclose(fileID);
fileID = []; % Not necessary
fprintf('Finished writing %d.\n', a);
winopen(fullFileName); % Open the file to look at it.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!