How to use variables as filename when saving to a .txt file?

조회 수: 16 (최근 30일)
Ross
Ross 2014년 9월 8일
댓글: Ross 2014년 9월 9일
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.

채택된 답변

Sean de Wolski
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
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.
Ross
Ross 2014년 9월 9일
Thanks guys that solved it for the filename. :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile 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!

Translated by