Structure fieldname Rename by referring a .csv column
7 次查看(过去 30 天)
显示 更早的评论
Hi , I would like to rename the workspace structure fieldnames as per the desired names that are present in the column 2 of the .csv file.
The table looks like this
Fieldname DesiredName
____________ ____________
'Test_Sig0' 'Test_Sig_0'
'Test_Sig' 'Test_Sig_1'
'Junk_Sig' 'Junk'
'Junk_Sig_1' 'Junk1'
While structure looks like this
Test_Sig0: {3×2 cell}
Test_Sig: {3×2 cell}
As told earlier , I would like to replace the fieldname 'Test_Sig0' with Desired name referred from the csv, 'Test_Sig_0' and delete the fieldname 'Test_Sig0'.
3 个评论
Image Analyst
2022-6-12
I've removed your flag. What is unclear. If it's your question, then make it clear with better explanations (don't flag it). If it's one of the Answers, ask the person to explain better.
回答(1 个)
Jan
2022-6-12
Import the table. Then:
Fieldname = {'Test_Sig0', 'Test_Sig', 'Junk_Sig', 'Junk_Sig_1'};
DesiredName = {'Test_Sig_0', 'Test_Sig_1', 'Junk', 'Junk1'};
S = RenameField(S, Fieldname, DesiredName)
2 个评论
Jan
2022-6-13
This is voodoo:
save('temp.mat', '-struct', 's'); % Save fields to a .mat file
clear all % Clear local variables (just for display purposes)
s=load('temp.mat');
clear all removes all loaded functions from the memory also and relaoding them fromthe slow disk is a massive waste of time.
I have posted a link to a function called RenameField already which does, what you want. Did you check it?
What is the purpose of this piece of code:
for j=1:numel(pos)
desired_ele=(model_test{pos})
end
desired_ele is overwritten in each iteration.
Does the posted code do, what you need or is there an open problem?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numeric Types 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!