필터 지우기
필터 지우기

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)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by