how do i change a variable name from workspace
99 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Tossawon Ngamnet
am 24 Jul. 2018
Bearbeitet: Tossawon Ngamnet
am 25 Jul. 2018
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/192462/image.jpeg)
i have a sequence of .mat files. When i load the mat files into workspace, the variable names are composed of sing or double underscore such as 'IR0004__, IR 10001_'. Thus, i would like to change these variables to be only number. How can i delete the underscore?
Please suggest Thank you
4 Kommentare
Stephen23
am 25 Jul. 2018
Bearbeitet: Stephen23
am 25 Jul. 2018
As I wrote in my comment, to load the data into one non-scalar array all variables in the .mat files must have exactly the same variables. This appears to not be the case with your .mat files, so you cannot use this method. If the variable names were exactly the same (as they should be) then you can simply do this:
for k = 1:N
S(k) = load(...)
end
As noted, this only works if the variable names are the same! Instead, you could use a cell array:
C = cell(1,N);
for k = 1:N
S = load(...);
C(1) = struct2cell(S);
end
Note that rather than this
['IR',num2str(fidx,'%04d'), '.mat']
you should use sprintf:
sprintf('IR%04d.mat',fidx)
Akzeptierte Antwort
KALYAN ACHARJYA
am 24 Jul. 2018
new_var_name=old_var_name;
clear('old_var_name');
4 Kommentare
Stephen23
am 24 Jul. 2018
Bearbeitet: Stephen23
am 24 Jul. 2018
"For example, using a complete code to rename all the files in my workspace"
Note that files are not in any MATLAB workspace, files are saved on your harddrive. Variables are in MATLAB workspace. Basic concepts, such as what variables are, are explained in the introductory tutorials:
Weitere Antworten (1)
Giridharan Kumaravelu
am 24 Jul. 2018
Bearbeitet: Giridharan Kumaravelu
am 24 Jul. 2018
getname = @(x) inputname(1);
IR0004__ = 10;
str = getname(IR0004__);
newstr = erase(str,'_');
eval([newstr '=' str ';']);
clear(str);
This could help you rename one variable. But if you are looking to rename the variables containing only numbers in the name, that will not be valid variable name in any programming language.
Hope this helps!
4 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!