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
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 .
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With