Assign to cell elements with both string and number?

3 vues (au cours des 30 derniers jours)
Khanh
Khanh le 3 Déc 2014
Commenté : Khanh le 9 Déc 2014
Hi ,
Could someone tell me how to assign to cell element with both string and number? For example with my simple code:
clc
close all
clear all
text={'STT'}
x=1:10
The result I want to get is
text{'STT';'1';'2';'3';...;'10'}
Thank you.
  2 commentaires
Andrew Newell
Andrew Newell le 3 Déc 2014
I presume you mean
text = {'STT';'1';'2';'3';...;'10'}
?
Khanh
Khanh le 3 Déc 2014
Sorry. I mean
text = {'STT';'1';'2';'3';'4';'5';'6';'7';'8';'9';'10'}

Connectez-vous pour commenter.

Réponse acceptée

Andrew Newell
Andrew Newell le 3 Déc 2014
Modifié(e) : Andrew Newell le 3 Déc 2014
You can use num2str to convert x to string; but you need to make it operate on one element of x at a time, or they will be bundled in one string. One approach is to use arrayfun:
text = [text; arrayfun(@num2str,x','UniformOutput',false)]
Note the transpose (') on the x, to make sure it is a column vector.
This answer was edited because I did not read the question correctly the first couple of times.
  1 commentaire
Khanh
Khanh le 9 Déc 2014
Hi Andrew,
Thank for your answer. It works on me. I am sorry for my late respond.

Connectez-vous pour commenter.

Plus de réponses (0)

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