Map & Match from .csv file and change Variable Name in Workspace
Infos
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Afficher commentaires plus anciens
SOMASHEKAR M
a ajouté un drapeau à question
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
Jeffrey Clark
le 12 Juin 2022
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
SOMASHEKAR M
le 12 Juin 2022
Jeffrey Clark
le 12 Juin 2022
@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
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!