Open an "array" of filenames

조회 수: 3 (최근 30일)
Alexander Horton
Alexander Horton 2023년 1월 9일
편집: VBBV 2023년 1월 10일
I have a need to open a variable number of files for output. Code later in the program will be conditionally writing data to these files. I wanted to be able to open these files in advance, instead of having to open and close them as each line is written resulting in a lot of opens and closes. I can't see a way to do this. I tried settign the filehandle as an array but that doesnt' work or I did it wrong e.g.
for i=1:totalfilesneeded
fhandle(i) = fopen(['OutputFile,i], 'w+');
end
Any way to programatically open several files and save their indexed handle names?
And, if possible, whats the limit on open files?

답변 (1개)

VBBV
VBBV 2023년 1월 10일
편집: VBBV 2023년 1월 10일
Its possible to open multiple files as you intend. Use the sprintf function to open individual filenames identified using an integer, as shown below
for i=1:totalfilesneeded
fhandle(i) = fopen(sprintf('OutputFile%d.txt',i), 'w+'); % a suitable file extension
end

카테고리

Help CenterFile 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!

Translated by