loop table to find specific criteria and create new table
Ältere Kommentare anzeigen
I have a large table where one variable is time (1st column). I want to write a code, that saves all the info from day to a .mat file. I can't just use For i=1:sth, as the number of data rows for one day differs. The may be 6 data rows for may 4th and then 0 for the 5th. I was trying to create a variable rows
for i=1:max(size(a_file_list))%max(size(a_file_list))
a_data_name = a_file_list(i).name;
a_data_mon = datenum(a_data_name(5:10), 'mmmyy');
a_file_path = [A_Data_loc filesep Depl_pos filesep a_file_list(i).name];
if a_data_mon >= dep_beg && a_data_mon <= dep_end % && is "and" in matlab
Data=readtable(a_file_path,'Delimiter',';','ReadVariableNames',true,'Format','%{dd-MM-yyyy}D %D %f %f %f %f %f %f %f %f %f %f %f %f %f','TreatAsEmpty','NA');
rows = ((datenum(Data.TIME)>dep_beg) & (datenum(Data.TIME)<dep_end)); % Sets the lines from the AIS-data that belong to the specified time window
Data = Data(rows, :); % Sorts out the AIS-data that belongs to the specified time window
But this code returns a table with 0 values. The code work fine without this part:
rows = ((datenum(Data.TIME)>dep_beg) & (datenum(Data.TIME)<dep_end)); % Sets the lines from the AIS-data that belong to the specified time window
Data = Data(rows, :); % Sorts out the AIS-data that belongs to the specified time window
But return a huge table, with all the values in the same table and .mat file. I need them separated by days and in different .mat files. If anybody has ideas, I would appreciate.
1 Kommentar
Image Analyst
am 29 Okt. 2015
What does a typical value for datenum(Data.TIME) look like, and what is the value for dep_beg? Are they both similar numbers? Can you snip out a few rows of a file and attach it? (We don't need the whole file of hundreds of megabytes or whatever. By the way, what is considered "huge" in your mind? How many millions of lines are in the table?)
Antworten (1)
John BG
am 29 Okt. 2015
1 Stimme
1.- instead of for i=1:max(size(a_file_list)) why not for i=1:(size(a_file_list,1))
2.- datenum does not have 'mmmyy', you may mean 'dd-mm-yy' but you have your own date format, made of 5 characters [5:10] inside each line of the file you want to chop, correct?
3.- why don't you initialize (before the for loop) the sink where you want to store data sorted by days with a cellarray that allows variable lengths: storage=cell(size(a_file_list,1),1)
4.- then find out all days first and store them on first column of the cellarray: storage(1)={'111yy'} storage(2)={'222yy''} ..
5.- then append each storage line accordingly: storage{1}='day1' storage{2}='day2' storage{3}='day3' storage{1,2}='entry1' storage{2,2}='entry2';storage{2,3}='entry3';storage{2,4}='entry4' storage{3,2}='entry5';storage{3,3}='entry6'
6.- and save each line of the cellarray to a different .mat file, for instance: for j=1:1:size(a_file_list,1) save(strcat('fileday',num2str(j),'.mat'),storage{j}) end
does it help?
John
Kategorien
Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!