How to generate a file name by combining variable characters based on the input of the matlab?

450 views (last 30 days)
Dear Matlab community,
Maybe somebody can help me with the following problem. For a project I would like to make file names based on the input of my matlab code. Therefore I first generate the name of the file and I was trying the following:
formatSpec = 'output_file_%s_value%.2f_%d_s%d.txt';
name='abc';
a_value = 0.90;
b_values = [20 10 -5 60];
c_value = 4;
output_file = sprintf(formatSpec,name,a_value,b_values,c_value);
Nevertheless, this is not working and I would like to achieve the following outcome in this example: output_file= output_file_abc_value0.90_20_10_-5_60_s4. Another complexity is that the length of the "b_value"-vector (in this case 4 values, but it could also be 2 or 10 values) changes based on the input of the program.
At the moment I don't know how I can generate this output with the varying length of the "b-values"-value.
Thank you.

Accepted Answer

Alex Mcaulley
Alex Mcaulley on 11 Mar 2019
Try this:
name='abc';
a_value = 0.90;
b_values = [20 10 -5 60];
c_value = 4;
output_file = ['output_file_' name '_value' num2str(a_value,'%.2f') '_' strrep(num2str(b_values(:)'), ' ', '_') '_s' num2str(c_value) '.txt'];

More Answers (2)

dpb
dpb on 11 Mar 2019
You're on right track...
fmt= 'output_file_%s_value%0.2f_%03d_s%03d.txt'; % use fixed width with leading zeros so will sort numerically
>> sprintf(fmt,name,a,b(1),c) % one value example
ans =
'output_file_abc_value0.90_020_s004.txt'
>> for i=1:numel(b) % for the array
sprintf(fmt,name,a,b(i),c)
end
ans =
'output_file_abc_value0.90_020_s004.txt'
ans =
'output_file_abc_value0.90_010_s004.txt'
ans =
'output_file_abc_value0.90_-05_s004.txt'
ans =
'output_file_abc_value0.90_060_s004.txt'
>>
For each element that is changed in any of the input variables you need to build a name. You can construct the loop to either loop through and generate all the names first and store in cellstr or string array or, just build the filename inside the overall calculation loop at the point you're ready to use it.
An alternative would be to use some other "helper" functions...
fnFName=@(B) sprintf(fmt,name,a,B,c) % anonymous function with the array argument
>> arrayfun(fnFName,b.','uni',0) % call with the array of b names
ans =
4×1 cell array
{'output_file_abc_value0.90_020_s004.txt'}
{'output_file_abc_value0.90_010_s004.txt'}
{'output_file_abc_value0.90_-05_s004.txt'}
{'output_file_abc_value0.90_060_s004.txt'}
>>
In the above, the other variables that are single-valued are inherent in the definition of the anonymous function at the time it is defined. If one of those is to be changed, then the anonymous function must also be redefined to reflect that change.

Steven Lord
Steven Lord on 11 Mar 2019
Edited: Steven Lord on 11 Mar 2019
If you're using a sufficiently recent release of MATLAB, you can combine string, char, and numeric data using +.
name='abc';
a_value = 0.90;
b_values = [20 10 -5 60];
c_value = 4;
outputfiles = "output_file_" + name + "_value" + a_value + "_" + ...
b_values + "_s" + c_value + ".txt"
You can even get fancier with implicit expansion. The following code creates a 2-by-4 string array.
a_value = [0.9; 0.8];
outputfiles = "output_file_" + name + "_value" + a_value + "_" + ...
b_values + "_s" + c_value + ".txt"
oneSelectedFile = outputfiles(2, 3)

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!