MATLAB Answers

how to open *.txt files with variable names

81 views (last 30 days)
hi I have a function '[dl]=funcall(x,y)' and in it I want to create *.text file with variable names and write out put vectors 'dl'. such as this
function [dl]=funcdl(x,y)
.........
fid=fopen('DL.txt', 'wt');
fprintf(fid, '%f\n',dl);
fprintf(fid, '\n');
fclose(fid);
......
but every time by calling function it creates the same name 'DL.txt' and overwrites out put!!! any one can help me???

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 13 Oct 2014
Edited: Stephen Cobeldick on 13 Oct 2014
  1 Comment
Stephen Cobeldick
Stephen Cobeldick on 13 Oct 2014
Try this, for example:
for k=1:3
str = sprintf('temp%d.txt',k);
fid = fopen(str,'wt');
...
... write data to your file here (fprintf, or whatever...)
...
fclose(fid);
end
EDIT: uses sprintf, based on the comments of Image Analyst and Robert Cumming.

Sign in to comment.

More Answers (1)

Gaurav Shukla
Gaurav Shukla on 13 Oct 2014
Consider an array
VarName = [A,B,C];
for i=1:3
str = strcat('fopen(''',VarName(i),'.txt','wt');
eval(str);
end
  14 Comments
Stephen Cobeldick
Stephen Cobeldick on 4 Jan 2015
For new readers, read all of these comments and
Do NOT use this code

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!