Reputation: 18753
I am very well aware on how to pre-allocate Matrix sizes using ones, zeros and cell command but what about a String ?
Suppose I have a Matrix named data
whose each value is between 1-255 now if i want to print these number's ASCII characters instead of numbers it selves, I'd do that,
msg='';
for i = 1 : length(data)
msg=horzcat(msg,floor(data(i))); % horzcat doesn't ignore spaces
end
msg
in the above code Matlab is unaware of the size of the msg
before the loop ends, What i really want to do is to declare the size of the variable msg
before the loop starts.
How can i do that ?
Upvotes: 2
Views: 8216
Reputation: 42225
You can use char
to preallocate a string just as you would a matrix (a string is just a char array):
msg = char(zeros(100,1));
However, this is probably not what you need (I haven't seen anyone preallocate a string for anything). Given that this is what you want to do
Suppose I have a Matrix named data whose each value is between 1-255 now if i want to print these number's ASCII characters instead of numbers it selves
you can simply do char(data)
to display the ASCII/Unicode values.
Upvotes: 4