38

Is it possibe to create an array of strings in MATLAB within a for loop?

For example,

for i=1:10
Names(i)='Sample Text';
end

I don't seem to be able to do it this way.

1
  • 4
    The reason your code doesn't work is that a string in MATLAB is a 1-D array of characters, so you are trying to squeeze 11 chars into one element of the array. You either need to use cells (which are basically arrays where each element only contains a pointer to some data, and that "some data" can well be a string) as proposed by Amro; or you need to use a 2-D char array as proposed by b3 (with the caveats discussed below Amro's answer ;-) ). Commented Aug 22, 2011 at 18:01

7 Answers 7

49

You need to use cell-arrays:

names = cell(10,1);
for i=1:10
    names{i} = ['Sample Text ' num2str(i)];
end
Sign up to request clarification or add additional context in comments.

3 Comments

It's also possible to do this with character arrays. See my answer.
@b3: assuming they are all of the same length (or padded with spaces to match)
Agreed, which is the case in the OP's question.
10

You can create a character array that does this via a loop:

>> for i=1:10
Names(i,:)='Sample Text';
end
>> Names

Names =

Sample Text
Sample Text
Sample Text
Sample Text
Sample Text
Sample Text
Sample Text
Sample Text
Sample Text
Sample Text

However, this would be better implemented using REPMAT:

>> Names = repmat('Sample Text', 10, 1)

Names =

Sample Text
Sample Text
Sample Text
Sample Text
Sample Text
Sample Text
Sample Text
Sample Text
Sample Text
Sample Text

Comments

9

Another option:

names = repmat({'Sample Text'}, 10, 1)

1 Comment

... which creates the same cell array as Amro, but with the technique used by b3 for a character array ;-).
3

New features have been added to MATLAB recently:

String arrays were introduced in R2016b (as Budo and gnovice already mentioned):

String arrays store pieces of text and provide a set of functions for working with text as data. You can index into, reshape, and concatenate strings arrays just as you can with arrays of any other type.

In addition, starting in R2017a, you can create a string using double quotes "".

Therefore if your MATLAB version is >= R2017a, the following will do:

for i = 1:3
    Names(i) = "Sample Text";
end

Check the output:

>> Names

Names = 

  1×3 string array

    "Sample Text"    "Sample Text"    "Sample Text"

No need to deal with cell arrays anymore.

Comments

2

Another solution to this old question is the new container string array, introduced in Matlab 2016b. From what I read in the official Matlab docs, this container resembles a cell-array and most of the array-related functions should work out of the box. For your case, new solution would be:

a=repmat('Some text', 10, 1);

This solution resembles a Rich C's solution applied to string array.

Comments

1

As already mentioned by Amro, the most concise way to do this is using cell arrays. However, Budo touched on the new string class introduced in version R2016b of MATLAB. Using this new object, you can very easily create an array of strings in a loop as follows:

for i = 1:10
  Names(i) = string('Sample Text');
end

2 Comments

This gives error: "Undefined function 'string' for input arguments of type 'char'."
@Danijel: This is an option in newer versions of MATLAB. I'm guessing you're​ using an older version.
0

one of the simplest ways to create a string matrix is as follow :

x = [ {'first string'} {'Second parameter} {'Third text'} {'Fourth component'} ]

2 Comments

But he explicitely asked for a way of doing it through a for loop.. Your method has to be manual, no way you can do this for 1k+ entries.
This solved my problem (was just looking for quick and dirty way to fill an array with hardcoded strings and this was the top google search result).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.