How to use multiple variables in a name structure

Hello,
a simple question please answer or show me a help file - I can't find it:
I get a filename
fig_name = [name, '_PLL.png'];
and get
'001_m1_60_const_20200611_200515_PLL.png'
now, I want have more variable in my fig_name, to get:
'001_m1_60_const_20200611_200515_SNR_1_PLL.png'
'001_m1_60_const_20200611_200515_SNR_2_PLL.png'
'001_m1_60_const_20200611_200515_SNR_3_PLL.png'
...
'001_m1_60_const_20200611_200515_SNR_N_PLL.png'
I tried allready with:
fig_name = [name,'_SNR_',SNR,'_PLL.png']
but I get just:
fig_name = '001_m1_60_const_20200611_200515_SNR__PLL.png'
What is wrong? What is a rule to create a names with usin of variables?
Thank you!

Antworten (2)

madhan ravi
madhan ravi am 23 Jun. 2020
compose('001_m1_60_const_20200611_200515_SNR_%d_PLL.png', (1:3).')

5 Kommentare

Thank you, but i want also change a name variable:
'001_m1_60_const_20200611_200515_SNR_1_PLL.png'
'001_m1_60_const_20200611_200515_SNR_2_PLL.png'
'001_m1_60_const_20200611_200515_SNR_3_PLL.png'
...
'001_m1_60_const_20200611_200515_SNR_N_PLL.png'
'002_foofoo_SNR_1_PLL.png'
'002_foofoo_SNR_2_PLL.png'
'002_foofoo_SNR_3_PLL.png'
.......
'002_foofoo_SNR_N_PLL.png'
'003_testtest_SNR_1_PLL.png'
............
so it should be 2 variables in fig_name: name and SNR. (two loops) It is possible?
Nik Rocky
Nik Rocky am 23 Jun. 2020
Bearbeitet: Nik Rocky am 23 Jun. 2020
I get it:
fig_name = [name,'_SNR_',num2str(SNR),'_PLL.png']
fig_name =
'001_m1_60_const_20200611_200515_SNR_2_PLL.png'
and for loop:
for i = 1:SNR
fig_name = [name,'_SNR_',num2str(i),'_PLL.png']
..........
end
Or for those of us who prefer sprintf():
for k = 1 : SNR
fig_name = sprintf('%s_SNR_%d_PLL.png', name, k)
end
Thank you sir Image Analyst.
Nik Rocky
Nik Rocky am 23 Jun. 2020
Yes, thank you!

Melden Sie sich an, um zu kommentieren.

Stephen23
Stephen23 am 23 Jun. 2020
Bearbeitet: Stephen23 am 23 Jun. 2020
The most efficient approach is to use sprintf (and is what experienced MATLAB users would do):
for k = ...
fig_name = sprintf('%s_SNR_%u_PLL.png',name,k);
...
end
You should avoid using i as a variable name, as this shadows the inbuilt imaginary unit i.

7 Kommentare

Note using i as a variable is not recommended.
Nik Rocky
Nik Rocky am 23 Jun. 2020
Yes, thank you very much!
%s = string ?
%u = uint/value ?
Stephen23
Stephen23 am 23 Jun. 2020
Bearbeitet: Stephen23 am 23 Jun. 2020
"Note using i as a variable is not recommended."
I guess that should be a comment to the OP here. As Madhan ravi notes, i is best avoided.
%s = string ?
%u = uint/value ?
Correct. Read more about the format options here:
Nik Rocky
Nik Rocky am 23 Jun. 2020
I found it
Stephen23
Stephen23 am 23 Jun. 2020
What you showed is a table for C's sprintf, apparently copied from here:
It supports a different set options than MATLAB's sprintf, although there is some overlap.
To know what options MATLAB's sprintf supports, refer to the MATLAB documentation:
Nik Rocky
Nik Rocky am 23 Jun. 2020
Oh, thank you! Its good to know. than the table is not compatible. Should I better delete it?

Melden Sie sich an, um zu kommentieren.

Tags

Gefragt:

am 23 Jun. 2020

Kommentiert:

am 23 Jun. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by