Filter löschen
Filter löschen

Accessing values of a field of a dataset

2 Ansichten (letzte 30 Tage)
Mark
Mark am 24 Okt. 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

Akzeptierte Antwort

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

Weitere Antworten (0)

Kategorien

Mehr zu Simulink Environment Customization finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by