MATLAB: How to add additional field in the structure from already formed cell array

cell arrayMATLABstructures

I want to add the additional field with (imported) data to include in already formed structure. I will use the example from the tutorial:
patient(1).name = 'John Doe';
patient(1).billing = 127.00;
patient(2).name = 'Ann Lane';
patient(2).billing = 28.50;
The data I want to include in the structure refer to cell array:
Name:
'John Doe'
'John Doe'
'John Doe'
'Ann Lane'
'Ann Lane'
'Ann Lane'
'Ann Lane'
'Ann Lane'
and related Score (8x1 double): [72 75 88 14 12 33 66 90].
I want to form a new field in patient structure (score) so that the [72, 75,88] match patient(1), i.e. John Doe and the rest to patient(2) i.e. Ann Lane.

Best Answer

  • patient(1).name = 'John Doe';
    patient(1).billing = 127.00;
    patient(2).name = 'Ann Lane';
    patient(2).billing = 28.50;
    name={'John Doe','John Doe','John Doe','Ann Lane','Ann Lane','Ann Lane','Ann Lane','Ann Lane'}
    score=num2cell( [72 75 88 14 12 33 66 90])
    nom={patient.name}
    for k=1:numel(score)
    p=patient(ismember(nom,name{k}))
    pa(k).name=p.name
    pa(k).billing=p.billing
    pa(k).score=score(k)
    end
    pa