How to use variables as filename when saving to a .txt file?
17 次查看(过去 30 天)
显示 更早的评论
Hi.
I have a program with many variables. I would like to use some of the variables as the filename when outputting to a .txt file. For example say I have variables:
Date = 22052014;
Time = 1422;
Machine = Alpha;
And I wish the filename to be:
Alpha_22052014_1422.txt
How can I do this? It's easy with the 'saveas' function but that is for images only to my knowledge and fopen doesn't appear to allow the same formatting.
Any help appreciated.
0 个评论
采纳的回答
Sean de Wolski
2014-9-8
I assume
Machine = 'Alpha'
Where Alpha is a string rather than variable. The easiest way is with sprint
Date = 22052014;
Time = 1422;
Machine = 'Alpha';
fn = sprintf('%s_%i_%i.txt',Machine,Date,Time)
2 个评论
Iain
2014-9-8
the functions you might want to use to write text files are the basic ones:
fopen, fprintf, fclose, etc.
But csvwrite might be up your street.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!