Rename Struct Fields Old Function
古いコメントを表示
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 件のコメント
per isakson
2022 年 6 月 8 日
Is this the error you get?
Scalar structure required for this assignment.
Error in renameStructField (line 19)
str.(newFieldName) = str.(oldFieldName);
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
2022 年 6 月 8 日
編集済み: Luck Haviland
2022 年 6 月 8 日
Luck Haviland
2022 年 6 月 8 日
Stephen23
2022 年 6 月 8 日
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
2022 年 6 月 8 日
採用された回答
その他の回答 (1 件)
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 についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!