trying to create a loop for for opening data files and plotting them
显示 更早的评论
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 个评论
From your indexing
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};
... etc
it appears that you want lots of named variables: perhaps a table would be easier, via readtable.
Susan Santiago
2018-9-29
"it won't let me upload a .dat file"
Append '.txt' as a new file extension, then upload the .dat file by clicking the paperclip button.
What MATLAB version are you using?
"I want to change it so that a different file is opened with each loop"
Please explain about the filenames: are they sequentially numbered? Or do they have random numbers? Or no numbers at all, only letters? What order do you want them loaded in? Are they all in one folder? Are they the only .dat files in that folder?
This is important information: we can easily tell you a method using dir that will read any random .dat files from some folder, but this might not gives the files you want and probably not in the order that you need. The better you can explain the filenames, the the more likely you will get the results that you are looking for: it would help is you gave a few example names, in the order that you want them loaded in.
Susan Santiago
2018-9-29
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.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!