Delete a line from a struct
55 views (last 30 days)
Show older comments
Hi, i have a structure with 9 lines and 4 fields. How can i delete line 4?
0 Comments
Answers (2)
Image Analyst
on 9 Dec 2017
To remove a field from a structure use rmfield(). For example if the structure is called mystruct and has fields field1, field2, field3, and field4
mystruct.field1 = rand();
mystruct.field2 = rand();
mystruct.field3 = rand();
mystruct.field4 = rand();
and you want to remove the field named "field4" you can do
mystruct = rmfield(mystruct, 'field4');
Structures don't have lines, unless what you mean is that a specification of a line (like a vector of points, or a slope and intercept) are stored in a field.
0 Comments
MeLearningProgramming
on 9 Jan 2020
structures can have lines: if you have
mystruct(1).field1 = rand();
mystruct(1).field2 = rand();
mystruct(1).field3 = rand();
mystruct(1).field4 = rand();
... till
mystruct(9).field1 = rand();
mystruct(9).field2 = rand();
mystruct(9).field3 = rand();
mystruct(9).field4 = rand();
you can remove a line like this:
mystruct(4) = [];
1 Comment
Image Analyst
on 9 Jan 2020
Well that would be very non-standard terminology. You've showed a structure array, or vector since it's 1-dimensional. It's a vector where each element is a structure. And each structure in the array is an element of the vector. I've never heard anyone call it a line. For example, with a double vector = [1,2,3,4,5], I've never heard someone call the 4th element a "line" instead. But maybe that's what the original poster meant.
See Also
Categories
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!