Map & Match from .csv file and change Variable Name in Workspace

  • SOMASHEKAR M a ajouté un drapeau Peu clair le 12 Juin 2022.
I would like to read a .mat file which consists of all structure field names eg a,b,c etc. I have a .csv file in which Column 1 has the same field names and in Column 2 I have the names to be replaced. Upon every instance of Column 1 names I would like to replace the workspace variabe name with corresponding column 2 value.

4 commentaires

You seem to be equivalencing 'structure field' and 'workspace variable' names - they are two different things. If you are trying to load a .mat file variable into a different named variable from your lookup table, I think you would have to first use something like matfile to map the .mat file then use the eval function to create the workspace new variable:
% Given: oldVar is a string or char array containing the original var name
% newVar is a string or char array containing the desired var name
% fileName is a string or char array containing the .mat filename
matMap = matfile(fileName);
eval([newVar '= matMap.' oldVar ';']);
If trying to rename a structure's fields maybe something like:
% Given: oldField is a string or char array containing the original field name
% newField is a string or char array containing the desired field name
% varName is the structure name
% fileName is a string or char array containing the .mat filename
matMap = matfile(fileName);
varName.(newField) = matMap.varName.(oldField); % loop/repeat this for all varName fields
Stephen23
Stephen23 le 12 Juin 2022
Modifié(e) : Stephen23 le 12 Juin 2022
"I would like to replace the workspace variabe name with corresponding column 2 value."
Are those variables of a table (i.e. the columns) or variables in the workspace?
Hi Stephen , 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'.
@SOMASHEKAR M, see my second example above IF you are talking about fields of a Structure array - MATLAB (mathworks.com). It would be something similar if you are talking about colums of a Table array with named variables that can contain different types - MATLAB (mathworks.com).

Réponses (0)

Cette question est clôturée.

Produits

Clôturé :

le 12 Juin 2022

Community Treasure Hunt

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

Start Hunting!

Translated by