Adding String variables to an Array of differing size

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 Kommentar

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?

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021a

Gefragt:

am 21 Aug. 2021

Kommentiert:

am 21 Aug. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by