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 件のコメント

Steven Lord
Steven Lord 2021 年 8 月 21 日
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?

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

製品

リリース

R2021a

タグ

質問済み:

2021 年 8 月 21 日

コメント済み:

2021 年 8 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by