trying to create a loop for for opening data files and plotting them
4 次查看(过去 30 天)
显示 更早的评论
this is the code I have so far. The part in the loop is from code I wrote for just one of the files. I want to change it so that a different file is opened with each loop but i'm not sure how. Thanks for any help
maxit=42;
i=1;
while i < maxit
A = textscan(fopen('7633_Flux_AmeriFluxFormat_3.dat'),'%f %f %f %f %f %f %f %f %f %f %f %f %f %f %s %s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %s %s %f %f %f %f %f %f %f %f', 'Delimiter',',','Headerlines',1);
A_time = A{1,1};
B_time = num2str(A_time);
x = datenum(B_time,'yyyymmddHHMM');
CO2 = A{1,3};
H2O = A{1,5};
FC = A{1,7};
LE = A{1,9};
ET = A{1,11};
H = A{1,13};
G = A{1,15};
SG = A{1,16};
WD = A{1,21};
WS = A{1,22};
ZL = A{1,25};
TA1 = A{1,36};
TA2 = A{1,39};
RH1 = A{1,37};
RH2 = A{1,40};
VPD = A{1,42};
SWin = A{1,52};
SWout = -A{1,53};
LWin = A{1,54};
LWout = -A{1,55};
ALB = A{1,50};
figure()
plot(x,CO2)
legend('CO2')
datetick('x','dd-mmm-yyyy HH:MM:SS','keeplimits','keepticks')
figure()
plot(x,H2O)
legend('H2O')
datetick('x','dd-mmm-yyyy HH:MM:SS','keeplimits','keepticks')
figure()
plot(x,FC)
legend('FC')
datetick('x','dd-mmm-yyyy HH:MM:SS','keeplimits','keepticks')
figure()
hold on
plot(x,LE)
plot(x,H)
legend('LE','H')
datetick('x','dd-mmm-yyyy HH:MM:SS','keeplimits','keepticks')
hold off
figure()
hold on
plot(x,G)
plot(x,SG)
legend('G','SG')
hold off
figure()
plot(x,WS)
legend('WS')
datetick('x','dd-mmm-yyyy HH:MM:SS','keeplimits','keepticks')
figure()
plot(x,ZL)
legend('ZL')
datetick('x','dd-mmm-yyyy HH:MM:SS','keeplimits','keepticks')
figure()
hold on
plot(x,TA1)
plot(x,TA2)
legend('TA1','TA2')
datetick('x','dd-mmm-yyyy HH:MM:SS','keeplimits','keepticks')
hold off
figure()
hold on
plot(x,RH1)
plot(x,RH2)
legend('RH1','RH2')
datetick('x','dd-mmm-yyyy HH:MM:SS','keeplimits','keepticks')
hold off
figure()
plot(x,VPD)
legend('VPD')
datetick('x','dd-mmm-yyyy HH:MM:SS','keeplimits','keepticks')
figure()
plot(x,ALB)
legend('ALB')
datetick('x','dd-mmm-yyyy HH:MM:SS','keeplimits','keepticks')
figure()
hold on
plot(x,SWin)
plot(x,SWout)
plot(x,LWin)
plot(x,LWout)
legend('SWin','SWout','LWin','LWout')
datetick('x','dd-mmm-yyyy HH:MM:SS','keeplimits','keepticks')
hold off
i=i+1;
end
5 个评论
Stephen23
2018-9-29
@Susan Santiago: thank you for uploading the file, that helped to understand the data.
Please also explain a little bit about the filenames, as I asked in my last comment. Your question is about how to read multiple files, and we have many choices for how to do this, but which choices are suitable depends on what you tell us about those filenames. Please check my last comment.
采纳的回答
Stephen23
2018-9-29
编辑:Stephen23
2018-9-29
"I want to change it so that a different file is opened with each loop but i'm not sure how"
By following the guidelines in the MATLAB documentation:
So you could either use dir to read the names from a folder, or generate the names using sprintf. Pick whichever works best for you. Here is an example with sprintf:
opt = {'Delimiter',',', 'Headerlines',1};
fmt = ['%s',repmat('%f',1,13),'%s%s',repmat('%f',1,29),'%s%s',repmat('%f',1,8)];
for k = 1:41
fnm = sprintf('7633_Flux_AmeriFluxFormat_%d.dat',k); % I had to guess the name format
[fid,msg] = fopen(fnm,'rt');
assert(fid>=3,msg)
A = textscan(fid,fmt,opt{:});
x = datenum(A{1,1},'yyyymmddHHMM'); % simpler
...
end
Note that you did not give any information on how the files names change, so I just guessed that the numbered suffix increments each time: if the names have some other pattern, you will have to specify that yourself, or give us a precise specification, or use dir.
Calling fopen directly inside textscan is an inspired move, but is not recommended: MATLAB does not automatically close those files, so you will end up with a large number of open files, which is not a good idea (it can even crash MATLAB if there are too many). So you should always use fclose if you fopen any file.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Large Files and Big Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!