Looping through list of files to access variable names
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi all, this might be very simple but i've been struggling to get solution for this all day. I have loaded in my mat file which contains variables e.g wind(500x1), temp(500x1), waves(500x1). I have created a list of the variables in the mat file using who. What i want to do is use the list of files to select each variable in turn, interpolate it and then define a new variable. I can't seem to get the cell string created by who to identify the variable in the workspace.
Licor_Datetime and Und_Datetime(500x1) are just two time vectors. I have no issue with the interpolation.
Any help would be appreciated
Richard
load ('C:\Users\rps207\Documents\MATLAB\CO2 NSOP output analysis\Data\DY030underway.mat')
Varnames = who('-file','C:\Users\rps207\Documents\MATLAB\CO2 NSOP output analysis\Data\DY030underway.mat');
%
for i=1:numel(Varnames)
Varnames_new= interp1(Und_Datetime,Varnames(i),Licor_Datetime);
end
Basic solution with no loops, tedious for 50+ variables
Salinity_new= interp1(Und_Datetime,Varnames(i),Licor_Datetime);
Waves_new= interp1(Und_Datetime,Waves,Licor_Datetime);
Temperature_new= interp1(Und_Datetime,Temperature,Licor_Datetime);
댓글 수: 1
Stephen23
2016년 6월 6일
@Richard Sims: Walter Roberson's answer is the best way to solve your question. In future you should keep in mind that it although it is possible to access variables like that, in practice it is very slow and buggy, and should be avoided. Here is an explanation of why you should not try to create or access lots of variable names dynamically:
채택된 답변
Walter Roberson
2016년 6월 3일
Assign the output of load() to a variable. The result will be a structure with one field for every variable in the .mat . If you want to check whether a particular variable is there you can use fieldnames() on the structure.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!