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

Is this the error you get?
Scalar structure required for this assignment.
Error in renameStructField (line 19)
str.(newFieldName) = str.(oldFieldName);
Jan
Jan 2022년 6월 8일
"I have found this function renameStructField" - please tell us, where you have found it. How can we help to fix this behavior in an unknown function?
Luck Haviland
Luck Haviland 2022년 6월 8일
편집: Luck Haviland 2022년 6월 8일
@Jan I have found it by perusing the internet. It appears to be a built-in MATLAB function. If you right click it in the MATLAB client and click "help on" it will give you minimal syntax.
and the person who mentioned it was Jos (10584).
Luck Haviland
Luck Haviland 2022년 6월 8일
Yes @per isakson that is correct.
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.
Luck Haviland
Luck Haviland 2022년 6월 8일
Hi Stephen23,
You have a wise point. However, I imported this data from an excel sheet where each header was prenamed with the unit of the dimension, as the data was from measuring via electronic micrometer the dimensions of individual specimens to be tested in shear.
Since the data was imported like this, I think it is difficult for me to rearrange it in a good data design way.
Thank you,
Luck

댓글을 달려면 로그인하십시오.

 채택된 답변

Jonas
Jonas 2022년 6월 8일

0 개 추천

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')

댓글 수: 1

Luck Haviland
Luck Haviland 2022년 6월 8일
That is unfortunate and I am very sad. Thank you for the answer Jonas.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2022년 6월 8일
편집: Jan 2022년 6월 8일

0 개 추천

Try this C-Mex verions:
An M-version is included also. I'm updating the M-version to accept string currently. It is coming in a few days.

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2022년 6월 8일

댓글:

2022년 6월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by