フィルターのクリア

how to access an array using a string?

6 ビュー (過去 30 日間)
na ja
na ja 2016 年 10 月 3 日
コメント済み: na ja 2016 年 10 月 5 日
I have 5 arrays which store the past 100 values related with 5 entities of a time series. time series contains of data values and each data value has one corresponding entity.
z = dataset('XLSFile','timeseries.xlsx');
var1_a = z(9698:9797);
var2_a = z(23298:23397);
var3_a = z(93372:93471);
var4_a = z(127688:127787);
var5_a = z(206878:206977);
% each array is of 100 elements.
% for future values of time series, I need to call one of the array to add
% 'new data value' to it and delete the oldest one out of it.
%I tried calling the array using 'strcat'.
same_val = strcmp(next element,'var1_a');
% 'next element' is any one from var1, var2, var3, var4 or var5.
% I need to access var1_a, var2_a.. and so on respectively for detected 'next element,.
if (condition is satisfied)
ary = strcat(next element,'_a');
ary = [ary(2:100);new data value];
else
(do something else)
end
but I am getting error for this. What should be the right way?

採用された回答

Star Strider
Star Strider 2016 年 10 月 3 日
I have no idea what you’re doing, but the way you are doing it is inefficient if not just wrong.
Use a cell array instead:
var_a{1} = z(9698:9797);
var_a{2} = z(23298:23397);
var_a{3} = z(93372:93471);
var_a{4} = z(127688:127787);
var_a{5} = z(206878:206977);
You can then access them easily in a loop.
  2 件のコメント
na ja
na ja 2016 年 10 月 3 日
thanks strider, that helped!
Star Strider
Star Strider 2016 年 10 月 3 日
My pleasure!

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

その他の回答 (2 件)

Joe Yeh
Joe Yeh 2016 年 10 月 3 日
You can store data in a structure
s = struct;
s.var1 = z(9698:9797);
s.var2 = z(23298:23397);
s.var3 = z(93372:93471);
s.var4 = z(127688:127787);
s.var5 = z(206878:206977);
The field name of a structure is by itself a string.
You can either access the field by :
s.var1
Or, if you store the string value in another variable, you can access the field by wrapping the variable in parentheses.
tempvar = 'var1';
s.(tempvar) % this is equal to s.var1
  2 件のコメント
na ja
na ja 2016 年 10 月 3 日
編集済み: na ja 2016 年 10 月 5 日
Hi Joe, I have s structure field named d.NL330260 and a temporary variable grade_n which is a 1x1 cell array containing 'NL330260'. I tried implementing the following:
grade_n = 'NL330260';
d.(grade_n)
but , I get this error message.
Argument to dynamic structure reference must evaluate to a valid field name.
What could be the problem??
na ja
na ja 2016 年 10 月 5 日
OK, solved it. I put it in this way:
grade_n = 'NL330260';
d.((char(grade_n) ))

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


Walter Roberson
Walter Roberson 2016 年 10 月 3 日
Cell array and structures are good ways to handle this. You can also consider using table() objects and using container.map()
  1 件のコメント
na ja
na ja 2016 年 10 月 5 日
thanks walter

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by