フィルターのクリア

Is there a way I can assign values to a vector of names?

1 回表示 (過去 30 日間)
Ana D Chavez Gonzalez
Ana D Chavez Gonzalez 2021 年 9 月 8 日
コメント済み: Matt J 2021 年 9 月 8 日
I want to have a vector of names, and use a for loop to give a value to each name.
For example, the bcvalues = {'bar01','bar01382,....}
for i = 1:length(bcvalues)
bcvalues(i) = NaN(1,30);
end
However, I obtained a "Conversion to cell from double is not possible."
Any kind of help is appreciated. Thank you.
  1 件のコメント
Ravi Narasimhan
Ravi Narasimhan 2021 年 9 月 8 日
編集済み: Ravi Narasimhan 2021 年 9 月 8 日
Matlab also allows "Associative Arrays" (e.g. dictionaries in Python) that can allow key/value pairs to be established.
% Define the keys
S={'bar01','bar01382'}
S = 1×2 cell array
{'bar01'} {'bar01382'}
% Map the keys to values; NaN(1,30) for the first and 2:10 for the second
mapObj = containers.Map(S,{nan(1,30), [2:10]})
mapObj =
Map with properties: Count: 2 KeyType: char ValueType: any
% Examine the values of the object using the keys
mapObj('bar01')
ans = 1×30
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
mapObj('bar01382')
ans = 1×9
2 3 4 5 6 7 8 9 10

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

採用された回答

Matt J
Matt J 2021 年 9 月 8 日
Yes, if you make the names the fields of a struct,
for i = 1:length(bcvalues)
S.(bcvalues{i}) = NaN(1,30);
end
  2 件のコメント
Ana D Chavez Gonzalez
Ana D Chavez Gonzalez 2021 年 9 月 8 日
Thank you.
Matt J
Matt J 2021 年 9 月 8 日
You're welcome but please Accept-click the answer to indicate that it resolved your question.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by