how to open *.txt files with variable names

20 ビュー (過去 30 日間)
zahra matlab
zahra matlab 2014 年 10 月 13 日
編集済み: Stephen23 2019 年 3 月 4 日
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???

採用された回答

Stephen23
Stephen23 2014 年 10 月 13 日
編集済み: Stephen23 2014 年 10 月 13 日
  1 件のコメント
Stephen23
Stephen23 2014 年 10 月 13 日
編集済み: Stephen23 2014 年 10 月 13 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Gaurav Shukla
Gaurav Shukla 2014 年 10 月 13 日
Consider an array
VarName = [A,B,C];
for i=1:3
str = strcat('fopen(''',VarName(i),'.txt','wt');
eval(str);
end
  14 件のコメント
Stephen23
Stephen23 2015 年 1 月 4 日
編集済み: Stephen23 2015 年 1 月 4 日
How about this example (from one of your own questions):
In a comment to one of the answers to that question you write "Its just that the code I inherited is just an absolute catastrophe when it comes to formatting and practice".
This seems to contradict your earlier comment on this thread: "I'm all for good coding practice, but at the same time when you need a fast, effect, simple, easy to understand solution for a quick job, sometimes being an elitist about your code is more of a hinderance and bottleneck then anything else".
And yet it seems that someone else's "fast, effect[ive], simple" solution became your "absolute catastrophe" problem to solve, which really did cause you a "hinderance and bottleneck" : not because it was well written code, but because it was poorly written code and because this is usually what poorly written and obfuscated code does.
MATLAB's inbuilt tools help to avoid basic syntax mistakes, which goes part of the way to writing good code. So, dear reader, why not use them?
Stephen23
Stephen23 2015 年 1 月 4 日
For new readers, read all of these comments and
Do NOT use this code

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by