Adding new fields to a dataset array?

조회 수: 9 (최근 30일)
Jinchuan
Jinchuan 2011년 6월 22일
Hi,
After I constructed a dataset array from a text file, I want to add a new column (variable) in the array called 'ID' and give a constant value to it in every row. Is there a way to specify a new field in the dataset array? For structure array I believe you can use "setfield" or "deal" to do it but it doesn't seems to work in dataset array. Here is what I've tried:
1. tmp.ID = {'100'}; This will create a new column called ID and give the value '100' only to the first row.
2. tmp.ID(:) = {'100'}; This will create a new column called ID with an empty cell for each row (no value given).
3. Now run both commands: tmp.ID = {'100'}; tmp.ID(:) = {'100'}; This time the ID value is given to every row.
Number 3 is the behavior I want although it doesn't seem to be the correct way to do it.
I'm new to the database array, any comments will be appreciated.

답변 (2개)

Laura Proctor
Laura Proctor 2011년 6월 22일
Try this:
tmp.ID = 100*ones(size(tmp,1),1);
  댓글 수: 1
Jinchuan
Jinchuan 2011년 6월 23일
Laura:
Thanks for your reply. I actually wants a string instead of a number, although along the same logic of yours the following code works:
tmp.ID = repmat('String',size(tmp,1),1);

댓글을 달려면 로그인하십시오.


Matt Fig
Matt Fig 2011년 6월 23일
tmp.ID(1:5) = {'100'}; % If 5 is the size you want.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by