/ / Nájsť, ktorý riadok súboru .dat MATLAB pracuje na - matlab, file-io

Nájdite, ktorý riadok súboru .dat MATLAB pracuje - matlab, file-io

Mám skript MATLAB, ktorý číta riadok z atextový súbor. Každý riadok textového súboru obsahuje názov súboru CSV. Potrebujem sledovať, na čom pracuje MATLAB, takže môžem uložiť dáta pre danú čiaru do bunkového poľa. Ako to môžem spraviť?

Na ilustráciu prvých pár riadkov môjho súboru .dat vyzerá takto:

2006-01-003-0010.mat
2006-01-027-0001.mat
2006-01-033-1002.mat
2006-01-051-0001.mat
2006-01-055-0011.mat
2006-01-069-0004.mat
2006-01-073-0023.mat
2006-01-073-1003.mat
2006-01-073-1005.mat
2006-01-073-1009.mat
2006-01-073-1010.mat
2006-01-073-2006.mat
2006-01-073-5002.mat
2006-01-073-5003.mat

Potrebujem uložiť premennú site_data z každého z nich .mat súbory do inej bunky O3_data, Preto potrebujem mať počítadlo tak, aby to bolo O3_data{1} sú údaje z prvého riadka textového súboru, O3_data{2} sú údaje z druhého riadka atď.

Tento kód funguje, ale to sa deje bez použitia počítadla, takže dostanem len údaje pre jeden zo súborov, ktoré som čítal:

year = 2006:2014;
for y = 1:9
flist = fopen(["MDA8_" num2str(year(y)) "_mat.dat"]); % Open the list of file names - CSV files of states with data under consideration
nt = 0; % Counter will go up one for each file loaded

while ~feof(flist) % While end of file has not been reached
fname = fgetl(flist);
disp(fname); % Stores name as string in fname
fid = fopen(fname);

while ~feof(fid)
currentLine = fgetl(fid);
load (fname, "site_data"); % Load current file. It is all the data for one site for one year
O3_data = site_data;
% Do other stuff
end
fclose(fid);
end
fclose(flist);
end

Ak pridám time index časť, MATLAB mi to hovorí Subscript indices must either be real positive integers or logicals. nt je celé číslo, takže neviem, čo robím zle. Potrebujem time index takže môžem mať O3_data {i}, v ktorých každý i je jeden zo súborov, ktoré čítam.

year = 2006:2014;
for y = 1:9
flist = fopen(["MDA8_O3_" num2str(year(y)) "_mat.dat"]); % Open the list of file names - CSV files of states with data under consideration
nt = 0;

while ~feof(flist) % While end of file has not been reached
fname = fgetl(flist);
fid = fopen(fname);

while ~feof(fid)
currentLine = fgetl(fid);
nt = nt+1; % Time index
load (fname, "site_data"); % Load current file. It is all the data for one site for one year
O3_data{nt} = site_data;
% Do other stuff
end
fclose(fid);
end
fclose(flist);
end

odpovede:

0 pre odpoveď č. 1

Vyskúšajte nasledujúce:

years = 2006:2014;
for y=1:numel(years)
% read list of filenames for this year (as a cell array of strings)
fid = fopen(sprintf("MDA8_O3_%d_mat.dat",years(y)), "rt");
fnames = textscan(fid, "%s");
fnames = fnames{1};
fclose(fid);

% load data from each MAT-file
O3_data = cell(numel(fnames),1);
for i=1:numel(fnames)
S = load(fnames{i}, "site_data");
O3_data{i} = S.site_data;
end

% do something with O3_data cell array ...
end

0 pre odpoveď č. 2

Skúste nasledujúce - berte na vedomie, že vzhľadom na to, že existuje vonkajší for slučka, nt premenná bude potrebné inicializovať mimo tejto slučky, aby sme neprepísali údaje z predchádzajúcich rokov (alebo predchádzajúcich j"s), môžeme sa vyhnúť vnútornému while slučka, pretože práve čítaný súbor je súbor * .mat a používame load príkaz na načítanie jeho jedinej premennej do pracovného priestoru.

year = 2006:2014;
nt   = 0;

data_03 = {};   % EDIT added this line to initialize to empty cell array
% note also the renaming from 03_data to data_03

for y = 1:9
% Open the list of file names - CSV files of states with data under
% consideration
flist = fopen(["MDA8_O3_" num2str(year(y)) "_mat.dat"]);

% make sure that the file identifier is valid
if flist>0

% While end of file has not been reached
while ~feof(flist)
% get the name of the *.mat file
fname = fgetl(flist);

% load the data into a temp structure
data = load(fname,"site_data");

% save the data to the cell array
nt = nt + 1;
data_03{nt} = data.site_data;
end

fclose(flist);  % EDIT moved this in to the if statement
end

end

Všimnite si, že vyššie uvedené predpokladá, že každý súbor * .dat obsahuje zoznam * .mat súborov, ako je znázornené vo vyššie uvedenom príklade.

Všimnite si EDIT vo vyššie uvedenom kóde od predchádzajúceho vysielania.