How do you add parameter values to filename?

Hello,
I am running a program in which an analysis is done using a set of 3 parameter values. How can save the output so that it will incorporate the values of the parameters in the filename?
For example, rep=250 num=1000 dx=20
are the initial values of the parameters.
I would like the filename to be 'rep=250-num=1000-dx=20'. And then when I go into the program next and change the parameter values at the beginning, I would like the filename to automatically change to reflect that new set of parameter values. How do I automate this?
I am using dlmwrite to save the output. Thank you so much!

답변 (2개)

Star Strider
Star Strider 2016년 8월 30일
편집: Star Strider 2016년 8월 30일

0 개 추천

Use sprintf to create the file name, then write to it:
file_name = sprintf('rep=%.0f-num=%.0f-dx=%.0f.txt', rep, num,dx);
fido = fopen(file_name,'wt');
... CODE ...
fclose(fido);
Note: This is UNTESTED CODE. It should work if your operating system allows such characters in file names.

댓글 수: 1

Guillaume
Guillaume 2016년 8월 30일
If the numbers are integer, %d is a better format than %.0f

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

Guillaume
Guillaume 2016년 8월 30일

0 개 추천

Use sprintf to build strings:
param1 = 250; param2 = 1000; param3 = 20;
filename = sprintf('rep=%d-num=%d-dx=%d.txt', param1, param2, param3);
dlmwrite(fullfile(folder, filename), somematrix);

댓글 수: 3

Duffie
Duffie 2016년 8월 30일
Thank you, Guillaume. That code worked perfectly!
I do have another question. If I want to have the program run 40 times, so I would have 40 versions of output from the same set of initial parameters, how could I add that version number to the filename as well?
So in the beginning, I would have for k=1:40 and then the program would run.
I would like that value of k to be in the filename also?
Thank you very much!
Do read the documentation of sprintf.
Simply add the parameter as an extra argument to sprintf and adjust the format string accordingly:
filename = sprintf('rep=%d-num=%d-dx=%d-k=%d.txt', param1, param2, param3, k);
Duffie
Duffie 2016년 8월 30일
Thank you again! I really appreciate it and will explore sprintf more.

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

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

태그

질문:

2016년 8월 30일

댓글:

2016년 8월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by