How to concat string + i?

189,187

Solution 1

You can concatenate strings using strcat. If you plan on concatenating numbers as strings, you must first use num2str to convert the numbers to strings.

Also, strings can't be stored in a vector or matrix, so f must be defined as a cell array, and must be indexed using { and } (instead of normal round brackets).

f = cell(N, 1);
for i=1:N
   f{i} = strcat('f', num2str(i));
end

Solution 2

For versions prior to R2014a...

One easy non-loop approach would be to use genvarname to create a cell array of strings:

>> N = 5;
>> f = genvarname(repmat({'f'}, 1, N), 'f')

f = 

    'f1'    'f2'    'f3'    'f4'    'f5'

For newer versions...

The function genvarname has been deprecated, so matlab.lang.makeUniqueStrings can be used instead in the following way to get the same output:

>> N = 5;
>> f = strrep(matlab.lang.makeUniqueStrings(repmat({'f'}, 1, N), 'f'), '_', '')

 f =
   1×5 cell array

     'f1'    'f2'    'f3'    'f4'    'f5'

Solution 3

Let me add another solution:

>> N = 5;
>> f = cellstr(num2str((1:N)', 'f%d'))
f = 
    'f1'
    'f2'
    'f3'
    'f4'
    'f5'

If N is more than two digits long (>= 10), you will start getting extra spaces. Add a call to strtrim(f) to get rid of them.


As a bonus, there is an undocumented built-in function sprintfc which nicely returns a cell arrays of strings:

>> N = 10;
>> f = sprintfc('f%d', 1:N)
f = 
    'f1'    'f2'    'f3'    'f4'    'f5'    'f6'    'f7'    'f8'    'f9'    'f10'

Solution 4

Using sprintf was already proposed by ldueck in a comment, but I think this is worth being an answer:

f(i) = sprintf('f%d', i);

This is in my opinion the most readable solution and also gives some nice flexibility (i.e. when you want to round a float value, use something like %.2f).

Solution 5

according to this it looks like you have to set "N" before trying to use it and it looks like it needs to be an int not string? Don't know much bout MatLab but just what i gathered from that site..hope it helps :)

Share:
189,187

Related videos on Youtube

simpatico
Author by

simpatico

Author of dp4j.jar, which lets you test access private methods in Java without writing any reflection API code. The necessary reflection code is injected by dp4j at compile-time. So you only write: @Test public void aTest(){ PrivateConstructor pc = new PrivateConstructor("Hello!"); Instead of: import java.lang.reflect.*; @Test public void aTest() throws IllegalAccessException, NoSuchMethodException , InvocationTargetException, InstantiationException { Constructor pcInit = PrivateConstructor.class.getDeclaredConstructor(String.class); pcInit.setAccessible(true); PrivateConstructor pc = (PrivateConstructor) pcInit.newInstance("Hello!"); Check it out at www.dp4j.com

Updated on October 02, 2020

Comments

  • simpatico
    simpatico over 3 years
    for i=1:N
       f(i) = 'f'+i;
    end
    

    gives an error in MatLab. What's the correct syntax to initialize an array with N strings of the pattern fi?

    It seems like even this is not working:

    for i=1:4
      f(i) = 'f';
    end
    
    • gnovice
      gnovice over 12 years
      Exactly what do you want the array f to look like? A single string with all of the individual fi strings concatenated together? A character array with one string per row? A cell array ?
    • bonanza
      bonanza over 5 years
      For recent version of MATLB, use strings. For example "asd" + (1:5) works then.
  • telenachos
    telenachos over 12 years
    Something like "f(i) = sprintf('%s%d', f, i);" would work as well.
  • simpatico
    simpatico over 12 years
    my issue was the lacking use of curly brackets to index
  • Mansoor Siddiqui
    Mansoor Siddiqui over 12 years
    This is actually a great solution. I wasn't aware of genvarname.
  • Oli
    Oli over 12 years
    ['f', num2str(i)] is a bit shorter.
  • Czechnology
    Czechnology over 6 years
    FYI: genvarname is deprecated.