フィルターのクリア

Accessing values of a field of a dataset

2 ビュー (過去 30 日間)
Mark
Mark 2011 年 10 月 24 日
Hi,
I need to access the values of a field in a dataset but the trick is that I don't know in advance the column name: it's passed to me in a string variable. I'm including below some code to give an idea of what I have in mind.
Thanks in advance
>> TestDSAccess % call the function below from Matlab's prompt
%%%%these two functions go in file TestDSAccess.m
function TestDSAccess()
myDS=dataset([1;2],[3;4],'VarNames',{'F1','F2'})
% this works but I don't know the variable name in advance (see below)
myDS.F1
% I will receive the variable name as a string
addToValue(myDS, 'F1', 7)
end
function result = addToValue(dsVar, strFieldName, number)
% this work but is too verbose and I have to store the result back in dsVar(:, {strFieldName})
temp = double( dsVar(:, {strFieldName}) )
result = temp + number ; %%Works
% this returns another dataset, not what I am looking for
temp = dsVar(:, {strFieldName})
result = temp + number ; %%ERROR!!
% something like the following would be ideal:
dsVar(:, {strFieldName}).data + number; % illegal
end

採用された回答

Daniel Shub
Daniel Shub 2011 年 10 月 25 日
You can use dynamic field names:
myDS=dataset([1;2],[3;4],'VarNames',{'F1','F2'});
strFieldName = 'F1';
myDS.(strFieldName)
  1 件のコメント
Mark
Mark 2011 年 10 月 25 日
Thank you, that worked.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSimulink Environment Customization についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by