Search code examples
matlabcell

Save output to cell


I want to save the output of my loop into a cell. Right now, it's all saving as one large cell, but I want the output after looking at each of the rows in WAS_open and WAS_close (i.e. the j counter) to be saved as a different cell inside the cell array.

O3_time = cell2mat(SARP2014_O3(:,2)); % Time data was taken
O3_data = cell2mat(SARP2014_O3(:,3)); % Data

% WAS
WAS_open = cell2mat(SARP2014_VertWAS(:,5)); % Open can time
WAS_close = cell2mat(SARP2014_VertWAS(:,6)); % Close can time
WAS_snake = cell2mat(SARP2014_VertWAS(:,1)); % Snake number

format long

k = 1;
for j = 1:length(WAS_open) % Number of observations for O3
    for i = 1:length(O3_time); % Number of cans
        if O3_time(i) >= WAS_open(j) && O3_time(i) <= WAS_close(j) % For all O3 between open and close times
            new_array{k}' = O3_data(i); % Save the O3 data that falls between the open and close times
            k = k+1;
        end
    end
end

So basically, in each row of SARP2014_VertWAS, there is a open time and a close time. I want to find the O3 data (which is measured every second) that falls between these two times and save it into a cell. So the data that falls between the open and close times in row 1 would be saved in new_array{1}, the data that falls between the open and close times in row 2 would be saved in new_array{2}, etc.

Then, I need to find the average of each of the cells and append that to SARP2014_VertWAS.

Snippet of the SARP2014_VertWAS arry:

Snake   Can      Flight Date    Open time           Close time
1901    7136    'RF219' 735774  735600.685763889    735600.686030093
1916    6057    'RF219' 735774  735600.686458333    735600.686932870
1917    8300    'RF219' 735774  735600.687152778    735600.687592593
1902    7169    'RF219' 735774  735600.687847222    735600.688310185
1915    6129    'RF219' 735774  735600.688541667    735600.688993056
1918    8279    'RF219' 735774  735600.689432870    735600.689872685
1903    7127    'RF219' 735774  735600.690277778    735600.690729167
1914    6102    'RF219' 735774  735600.693402778    735600.693796296
1919    8203    'RF219' 735774  735600.693877315    735600.694259259
1904    7203    'RF219' 735774  735600.694328704    735600.694699074

Snippet of the SARP2014_O3 array:

735773  735600.690659722    0.521049000000000
735773  735600.690671296    0.521049000000000
735773  735600.690682870    0.521049000000000
735773  735600.690694445    1.42528000000000
735773  735600.690706019    1.42528000000000
735773  735600.690717593    1.42528000000000
735773  735600.690729167    1.42528000000000
735773  735600.690740741    2.39701000000000
735773  735600.690752315    2.39701000000000
735773  735600.690763889    2.39701000000000
735773  735600.690775463    NaN

Solution

  • Pull out the data and keep track of how many points are between the open and close times of each snake. Then, find the average using what was found (the number of points there are) as an index.

    k = 1;
    num_of_pts = zeros(120,1);
    O3_WAS = zeros(5329,1);
    for j = 1:length(WAS_open) % Number of observations for O3
        tick = 0;
        for i = 1:length(O3_time); % Number of cans
            if O3_time(i) >= WAS_open(j) && O3_time(i) <= WAS_close(j) % For all O3 between open and close times
                O3_WAS(k) = O3_data(i); % Save all O3 data from that WAS can to a new array
                array(j) = O3_data(i); % Save all O3 data from that WAS can to a new array
                tick = tick+1; 
                % avg_O3(j) = nanmean(new_array(k)); % Average all O3 data from that can
                k = k+1; % Advance the counter
            end
        end
        num_of_pts(j) = tick;
    end
    
    avg_O3 = zeros(120,1);
    % Find the average O3 during the interval each can was open
    for n = 1:length(WAS_open-1)
        for i = 1:sum(num_of_pts)
            if n == 1
                avg_O3(1) = nanmean(O3_WAS(1:num_of_pts(n))); % First one
            else
                avg_O3(n) = nanmean(O3_WAS(num_of_pts(n-1)+1:((num_of_pts(n-1)+num_of_pts(n)))));
            end
        end
    end