Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concat string + i?

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 
like image 874
simpatico Avatar asked Dec 07 '11 17:12

simpatico


People also ask

Can you use += for string concatenation?

The + Operator The same + operator you use for adding two numbers can be used to concatenate two strings. You can also use += , where a += b is a shorthand for a = a + b .

Can you concatenate strings in C++?

Syntax: string new_string = string init + string add; This is the most easiest method for concatenation of two string. The + operator simply adds the two string and returns a concatenated string.

Can we concat string and int?

To concatenate a string to an int value, use the concatenation operator. Here is our int. int val = 3; Now, to concatenate a string, you need to declare a string and use the + operator.


1 Answers

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 
like image 92
Mansoor Siddiqui Avatar answered Sep 20 '22 01:09

Mansoor Siddiqui