create txt files with specific names

16 visualizaciones (últimos 30 días)
jason lee
jason lee el 13 de Jul. de 2020
Editada: Stephen23 el 13 de Jul. de 2020
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 comentarios
jason lee
jason lee el 13 de Jul. de 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 el 13 de Jul. de 2020
Editada: Stephen23 el 13 de Jul. de 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

Iniciar sesión para comentar.

Respuesta aceptada

madhan ravi
madhan ravi el 13 de Jul. de 2020
Editada: madhan ravi el 13 de Jul. de 2020
for ii = 3:-1:1
Use sprintf() to generate filenames:
sprintf('RSN%d.txt', ii)
  2 comentarios
jason lee
jason lee el 13 de Jul. de 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 el 13 de Jul. de 2020
“sometimes, the file name could be rather complex, so i want a more general way.”
sprintf() is the general way.
“why?”
No idea.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by