Adding String variables to an Array of differing size

1 view (last 30 days)
I'm coding for a personal project and I've ran into a roadblock for me. I want to add string values to an array that changes size depending on how many values there are. Here's an example:
str_val_init = ["Hello","World","How","Are","You"];
str_val_need = zeros([1 x])
x = 0;
num = [2,3,3] %the real num array is random
for idx = 1:numel(num)
ind = num(idx)
if ind == 2
x = x + 1
str_val_need = str_val_init(1,1);
elseif ind == 3
x = x + 1
str_val_need = str_val_init(1,2)
else
end
end
I keep getting the error "Unable to perform assignment because value of type 'string' is not convertible to 'function_handle'." Really I just want to know how to continue adding string values to an array that changes size.
  1 Comment
Steven Lord
Steven Lord on 21 Aug 2021
This seems quite similar to this other Answers post in which Cris LaPierre and I have posted answers. Perhaps you can use what you learned in that discussion here?

Sign in to comment.

Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!