trying to create a loop for for opening data files and plotting them

4 Ansichten (letzte 30 Tage)
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 Kommentare
Susan Santiago
Susan Santiago am 29 Sep. 2018
2016 but I uploaded the matrix so you can see where those variables are coming from since you asked about it
Stephen23
Stephen23 am 29 Sep. 2018
@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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 29 Sep. 2018
Bearbeitet: Stephen23 am 29 Sep. 2018
"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.
Note that your code could be significantly simplified by using readtable.
  2 Kommentare
Susan Santiago
Susan Santiago am 29 Sep. 2018
the names of the files are similar to '7633_Flux_AmeriFluxFormat_3.dat' the only change is the last number changes from 0-4. a lot of the files have the same name though so they're saved as things such as '7633_Flux_AmeriFluxFormat_2 (1).dat' also but the '7633_Flux_AmeriFluxFormat_.dat' part remains the same in all of the files
Stephen23
Stephen23 am 29 Sep. 2018
Bearbeitet: Stephen23 am 29 Sep. 2018
To get those files in numeric order you could download my FEX submission natsortfiles:
There are plenty of examples in the help, in the HTML documentation, and in the online description.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Large Files and Big Data finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by