Error using horzcat with num2str function for creating character class

1 vue (au cours des 30 derniers jours)
>>I am not understanding why the for double figure value, it is creating problems. Could anyone please help me with the situation?
Nt=10;Bd=9;Ns=1;
Str=['psd_c_',num2str(Nt),'_by_',num2str(Bd)','_shaded_',num2str(Ns)]
Str =
psd_c_10_by_9_shaded_1
But,
Nt=10;Bd=10;Ns=1;
Str=['psd_c_',num2str(Nt),'_by_',num2str(Bd)','_shaded_',num2str(Ns)]
Error using horzcat
Dimensions of matrices being concatenated are not consistent.

Réponse acceptée

madhan ravi
madhan ravi le 20 Fév 2019
Str = sprintf('psd_c_%d_by_%d_shaded_%d',Nt,Bd,Ns)
doc sprintf % read it
  2 commentaires
Stephen23
Stephen23 le 20 Fév 2019
MD RAQUIBUZZAMAN'S "Answer" moved here:
Thank you. very much.
Stephen23
Stephen23 le 20 Fév 2019
@MD RAQUIBUZZAMAN: you should accept madhan ravi's answer if it resolves your question.

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 20 Fév 2019
You have one extra ' after your second num2str call. That's not a problem when Bd is a number with just one digit (the transpose of '5' is '5', for example) but becomes a problem when Bd is not an integer between 0 and 9 inclusive (the transpose of '10' has two rows.)
There are a couple possible solutions:
  • You could remove that extra '.
  • You could use sprintf as madhan ravi suggested.
  • You could use a string array as shown below.
Nt=10;
Bd=10;
Ns=1;
Str="psd_c_" + Nt + "_by_" + Bd + "_shaded_" + Ns
Depending on the release you're using, many of the functions that accept char array inputs will accept string inputs. If I had to guess I'd say you want to use this as a file name or as the title, xlabel, ylabel, etc. of an axes. At least in release R2018b that should work though you'd want to specify the 'Interpreter' property of the title so the underscore characters are displayed as underscores not interpreted as subscripts.
title(Str, 'Interpreter', 'none')
  1 commentaire
MD RAQUIBUZZAMAN
MD RAQUIBUZZAMAN le 20 Fév 2019
Yeah. Now it is clear to me. Thank you for the clear explanation.

Connectez-vous pour commenter.

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