Clear Filters
Clear Filters

Adding fields to a struct?

52 views (last 30 days)
Nicholas Kavouris
Nicholas Kavouris on 8 Aug 2022
Answered: Bruno Luong on 8 Aug 2022
I have an existing struct titled PR1Data. I would like to add a new field titled 'filename' to the structure, why is this so difficult to do? I only want the field to exist, i have tried the following methods with no results:
PR1Data=setfield(PR1Data,'filename',[1 2])
error: 'scalar structure required for this assignment'
Same error
How can i simply add a new field to an existing struct?? this should not be this difficult

Accepted Answer

Bruno Luong
Bruno Luong on 8 Aug 2022
[PR1Data.filename] = deal('');
or if you PR1Data is non empty
PR1Data(1).filename = '';

More Answers (0)


Find more on Structures in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!