Best way to rename a loaded variable?
이전 댓글 표시
Say that I have a .mat file that contains a variable, and I need to load this variable and give it a different name. Is there any other (/better) way to do this than:
load(myFile, myVar)
eval(['myNewname = ' myVar '; clear ' myVar])
?
댓글 수: 2
Jan
2012년 11월 27일
Where does this EVAL idea come from? Did you find EVAL in the documentation or did you see it in an example?
I ask, because it is such frequently suggested to avoid EVAL in this and other Matlab forums for so many years, that I actually expect, that this method should be extinct already.
Alec Nagel
2012년 11월 27일
채택된 답변
추가 답변 (2개)
Image Analyst
2015년 2월 18일
I'd do it this way:
storedStructure = load(myFile, 'myVar'); % Load in ONLY the myVar variable.
myNewname = storedStructure.myVar; % Assign it to a new variable with different name.
clear('storedStructure'); % If it's really not needed any longer.
Same net effect, it just uses the names Alec gave, and avoids dynamic structure fields, which are a bit advanced for beginners and not necessary here if you know the actual name.
Stefano Petrò
2022년 3월 17일
0 개 추천
A way to do this in a single command is
myNewname = getfield(load(myFile,myVar),myVar);
댓글 수: 1
myVar = 'cdate';
C = load('census.mat', myVar).(myVar)
카테고리
도움말 센터 및 File Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!