Azzera filtri
Azzera filtri

How create string like this: word+number+word with number=num2str(x)

240 visualizzazioni (ultimi 30 giorni)
Hi everyone,
how could I write a string like the following word+number+word with number=num2str(x). For example:
x=2; x_str=num2str(x); y='I have' x_str 'dogs'
Thank you for your help.

Risposta accettata

Santhana Raj
Santhana Raj il 9 Mag 2017
x=2;
y= ['I have' num2str(x) 'dogs'];
  6 Commenti
Steven Lord
Steven Lord il 9 Mag 2017
If you're using a release that includes the string data type and you specify the formatSpec input to sprintf as a string rather than a char vector, you will receive a string rather than a char vector as output.
A = sprintf(string('abc %d def'), 17);
B = sprintf("abc %d def", 17); % release R2017a and later

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 9 Mag 2017
y = sprintf('I have %d dogs', x);
  2 Commenti
Gennaro Arguzzi
Gennaro Arguzzi il 9 Mag 2017
Hi @Walter Roberson, y is a character vector (e.g. y=sprintf(2222) has 1x4 dimensions). My goal is to obtain a string.
Walter Roberson
Walter Roberson il 9 Mag 2017
MATLAB does not have a string data type before R2016b, on character vectors. It is common to put character vectors inside cell arrays, such as
{sprintf('I have %d dogs', x), 'I have no cats', sprintf('I have %d iguanas', y) }

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by