フィルターのクリア

Structure fieldname Rename by referring a .csv column

7 ビュー (過去 30 日間)
SOMASHEKAR M
SOMASHEKAR M 2022 年 6 月 12 日
コメント済み: Jan 2022 年 6 月 13 日
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 件のコメント
SOMASHEKAR M
SOMASHEKAR M 2022 年 6 月 12 日
I am able to import the table
Image Analyst
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
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 件のコメント
SOMASHEKAR M
SOMASHEKAR M 2022 年 6 月 12 日
編集済み: Jan 2022 年 6 月 13 日
I am quite new to MTALB , I have a long list of elements that has to be renamed and hence I am using the excel sheet to do it. Kindly refer the code below that I tried. Kindly recommend for changes based on the Excel reference only.
clear
clc
s.Test_Sig0={1,0.1;2,0.2;3,0.3};
s.Test_Sig1={1,0.1;2,0.2;3,0.3};
s.Junk_Sig_1={1,0.1;2,0.2;3,0.3};
save('temp.mat', '-struct', 's'); % Save fields to a .mat file
clear all % Clear local variables (just for display purposes)
s=load('temp.mat');
A=readtable('test_matrix.xlsx');
variables=A.Properties.VariableNames; %get variable names
B=fieldnames(s);
%B = struct2table(B);
out=[];
desired_ele=0;
for i=variables
myquery = 'CANName';
if ismember(myquery, A.Properties.VariableNames)
col = find(strcmp(myquery, A.Properties.VariableNames));
test=A(:,col);
test=table2array(test);
[val,pos]=intersect(test,B,'stable');
myquery = 'ModelName';
if ismember(myquery, A.Properties.VariableNames)
col = find(strcmp(myquery, A.Properties.VariableNames));
model=A(:,col);
model_test=table2array(model);
z=numel(pos);
end
end
%end
%end
end
% Append the Desired Element into the array
for j=1:numel(pos)
desired_ele=(model_test{pos})
end
%end
%Replace Ola Fieldname with New Fieldname
%matMap = matfile('temp.mat');
%s.Test_Sig_0 = s.('Test_Sig0'); % loop/repeat this for all varName fields
Jan
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 ExchangeNumeric Types についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by