Azzera filtri
Azzera filtri

Accessing values of a field of a dataset

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Daniel Shub
Daniel Shub il 25 Ott 2011
You can use dynamic field names:
myDS=dataset([1;2],[3;4],'VarNames',{'F1','F2'});
strFieldName = 'F1';
myDS.(strFieldName)

Più risposte (0)

Categorie

Scopri di più su Simulink Environment Customization in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by