Effacer les filtres
Effacer les filtres

construc a string from other strings and variables

7 vues (au cours des 30 derniers jours)
Jeff Eriksen
Jeff Eriksen le 10 Fév 2014
Modifié(e) : per isakson le 11 Fév 2014
I am just trying to construct a single string to use as a file name, from several other strings and variables. Should be simple, but I cannot get the syntax right. For example:
subj = 'A2';
ndip = '2503';
nroi = '68';
band = '2-4';
fil_nam = [subj '_b' band '_d' ndip '_s' nroi];
fil_nam is created as a cell array, so how do I force it to be a string? Or convert to string?
Thanks
  1 commentaire
Walter Roberson
Walter Roberson le 10 Fév 2014
Please re-check your actual code to see if you possibly used {} instead of [] ?

Connectez-vous pour commenter.

Réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 10 Fév 2014
If fil_nam is a cell array
fil_nam = {subj '_b' band '_d' ndip '_s' nroi}
out=strjoin(fil_nam)
  2 commentaires
Walter Roberson
Walter Roberson le 10 Fév 2014
strjoin() puts a space between the parts by default. You can specify '' as the delimiter to avoid the space. If you are going to do that then you might as well use
out = cat(2, fil_name{:});
Azzi Abdelmalek
Azzi Abdelmalek le 10 Fév 2014
Exact, or we can use
out=[fil_nam{:}]

Connectez-vous pour commenter.


per isakson
per isakson le 10 Fév 2014
Modifié(e) : per isakson le 11 Fév 2014
I find sprintf to be the best function to combine fixed strings and values of string (and numeric) variables into one string. Thus,
sprintf( '%s_b%s_d%s_s%s', subj, band, ndip, nroi )
returns
ans =
A2_b2-4_d2503_s68

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by