MATLAB Answers

create txt files with specific names

조회 수: 9(최근 30일)
i want to create a series of txt files with predefined char names.
here is my code
s = {'RSN1','RSN2','RSN3'};
for i = 3:1:1
fopen([s{i},'.txt'],'w');
end
unfortunately, it dosen't work.
but if i input
s = {'RSN1','RSN2','RSN3'};
fileID = fopen([s{1},'.txt'],'w');
it worked, a txt file named 'RSN1.txt' is created.
so, why it dosen't works with a for loop?
thank you

  댓글 수: 2

jason lee
jason lee 13 Jul 2020
emm, here i updated my problem because some issues have been solved by trying.
here is my code
s = {'RSN1','RSN2','RSN3'};
for i = 1:3
fileID(i) = fopen([s{i},'.txt'],'w');
fprintf(fileID(i),'%d',i);
fclose(fileID(i));
end
in this way, one can create a series of txt files with pre-defined name.
a problem is how to pre-allocate fileID for its value will be changed within a for loop.
anyway, if you have any other better way to create a series of files with specified names,
pls indeed tell me.
by the way, the syntax
fileID(i) = fopen([s{i},'.txt'],'w');
can not be found in MATLAB documentations, but it works with no warning or errors,
why?
thank you!
Stephen Cobeldick
Stephen Cobeldick 13 Jul 2020
Have you looked at how many values this vectors actually has (hint: zero):
for i = 3:1:1
% ^^^^^ how many elements does this have (hint: zero)
Because that vector has zero values, you loop iterates zero times. Probably you want to make the step -1.
"by the way, the syntax fileID(i) = fopen([s{i},'.txt'],'w'); can not be found in MATLAB documentations, but it works with no warning or errors"
I don't see any obvious reason why it should throw any error or show any warning. In any case it is explained in the documentation: array concatenation is explained here:
and fopen is unsurprisingly explained in the fopen documentation. The fact that you combined array concatenation and fopen is not something that needs to be documented (nor could it be documented, it would be impossible to document every permutation of all MATLAB operations).
"a problem is how to pre-allocate fileID for its value will be changed within a for loop."
You don't need to, and in fact there is absolutely no point in your storing the file ID from every file that you open: because you close those files within the same loop iteration the file IDs are useless (ltierally you cannot do anything with them), so there is no point to keeping them hanging around.
s = {'RSN1','RSN2','RSN3'};
for k = 1:numel(s)
fnm = sprintf('%s.txt',s{k});
fid = fopen(fnm,'w');
fprintf(fid,'%d',k);
fclose(fid);
end

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

채택된 답변

madhan ravi
madhan ravi 13 Jul 2020
편집: madhan ravi 13 Jul 2020
for ii = 3:-1:1
Use sprintf() to generate filenames:
sprintf('RSN%d.txt', ii)

  댓글 수: 2

jason lee
jason lee 13 Jul 2020
indeed,
thank you!
well, this is just a demo. sometimes, the file name could be rather complex, so i want a more general way.
what i found is to pre-define a series of names and use 'fopen' to create them.
if you have any other better way, pls share with me!
by the way, the syntax
fileID(i) = fopen([s{i},'.txt'],'w');
can not be found in MATLAB documentations, but it works with no warning or errors,
why?
thank you!
madhan ravi
madhan ravi 13 Jul 2020
“sometimes, the file name could be rather complex, so i want a more general way.”
sprintf() is the general way.
“why?”
No idea.

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

추가 답변(0개)

태그

제품


릴리스

R2019b

Translated by