create txt files with specific names

22 views (last 30 days)
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 Comments
jason lee
jason lee on 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!
Stephen23
Stephen23 on 13 Jul 2020
Edited: Stephen23 on 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

Sign in to comment.

Accepted Answer

madhan ravi
madhan ravi on 13 Jul 2020
Edited: madhan ravi on 13 Jul 2020
for ii = 3:-1:1
Use sprintf() to generate filenames:
sprintf('RSN%d.txt', ii)
  2 Comments
jason lee
jason lee on 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 on 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!