Rename Struct Fields Old Function
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Luck Haviland
el 8 de Jun. de 2022
Comentada: Luck Haviland
el 8 de Jun. de 2022
Hello,
I have a struct that has 20 different field names. Somewhere within these 20 names, it has a field name of B_in_ which I want to rename to B_m because I converted its value from inches to meters. I have found this function renameStructField, but it requires a scalar structure for the renaming. I tried this:
a="B_in_"
b="B_m"
FullTable=renameStructField(FullTable,a,b)
Interestingly if I were to do isscalar(a), and isscalar(b) they both return 1 which means they are both scalars. However, it still throws the scalar structure error.
Anyone know why this does not work, and how to make it work?
Thank you,
Luck
6 comentarios
Stephen23
el 8 de Jun. de 2022
So far no one has mentioned the role of data design: this question is a good illustration of why forcing meta-data into fieldnames (or even worse, variable names) makes it harder to process that data. For a detailed explanation:
Better data design relies on understanding that meta-data is data, and data is best stored in a variable, not in its name:
S.B_val = 3.4;
S.B_unit = 'in';
Note how much easier this is to work with, compared to fiddling around with fieldnames.
Respuesta aceptada
Jonas
el 8 de Jun. de 2022
the question is not if your fieldnames are scalar but if the struct is scalar. you will not have any problems if the atruct has multiple fields but is one dimensional:
myStructScalar.inIn=5;
renameStructField(myStructScalar,'inIn','inM')
but if your struct is not scalar, this will not work directly
myStruct(1).inIn=2
myStruct(2).inIn=6
renameStructField(myStruct,'inIn','inM')
jowever you can vreate a new field and do the conversion in the same step and remove the old field afterwards
for structNr=1:numel(myStruct)
myStruct(structNr).inM=myStruct(structNr).inIn;
end
rmfield(myStruct,'inIn')
Ver también
Categorías
Más información sobre Structures en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!