Delete a line from a struct

61 次查看(过去 30 天)
José Pereira
José Pereira 2017-12-9
Hi, i have a structure with 9 lines and 4 fields. How can i delete line 4?

回答(2 个)

Image Analyst
Image Analyst 2017-12-9
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.

MeLearningProgramming
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 个评论
Image Analyst
Image Analyst 2020-1-9
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by