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

concatenate textfile namevariable characters

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.

Best Answer

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'];