Effacer les filtres
Effacer les filtres

Accessing values of a field of a dataset

2 vues (au cours des 30 derniers jours)
Mark
Mark le 24 Oct 2011
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

Réponse acceptée

Daniel Shub
Daniel Shub le 25 Oct 2011
You can use dynamic field names:
myDS=dataset([1;2],[3;4],'VarNames',{'F1','F2'});
strFieldName = 'F1';
myDS.(strFieldName)
  1 commentaire
Mark
Mark le 25 Oct 2011
Thank you, that worked.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Simulink Environment Customization dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by