Export variables from excel based on a defined column category and time

3 views (last 30 days)
I would like to select and export a subset of values from an excel document based on both a category and time.
An example of the data is attached. Column A represents a time, column B represents a text event, and columns E-L represent individual datapoints for analysis.
What I would like to do, is when an event is defined (for example, 'Baseline' [B8]), to collect the values every 1 minute for columns E, G, I and K, until the next event is defined (for example 'Succinate' [B40]), and continue to collect every 1 minute until the next event ('Reperfusion' [B615]) until the end of the experiment, then export these values into a separate csv file.
At the moment I am doing this manually and it is extremely time consuming, especially as I have over 300 files to analyse, so I am going slightly insane. All files are formatted in a similar way, but are of different durations in time depending on the experiment. Does anyone know if this is possible using MATLAB? Any advice immensely appreciated!
Annabel

Accepted Answer

Voss
Voss on 2 Aug 2023
Edited: Voss on 2 Aug 2023
Something like this would work, if the exact times you're looking for (i.e., every 1 minute starting at an event, until the next event/end of file) are really always there, which they are in this particular example file.
input_file_name = 'Example_Experiment_2023-08-02.xlsx';
output_file_name = 'data_subset.csv';
T = readtable(input_file_name,'VariableNamingRule','preserve');
cols = [1 2 5 7 9 11]; % I'm including columns A and B in the output file too - feel free to modify
all_times = T{:,1};
event_rows = find(~cellfun(@isempty,T{:,2}));
N_events = numel(event_rows);
event_rows(end+1) = size(T,1);
data = cell(1,N_events);
for ii = 1:N_events
times = all_times(event_rows(ii)):all_times(event_rows(ii+1));
[~,idx] = ismember(times,all_times);
data{ii} = T(idx,cols);
end
T_new = vertcat(data{:});
writetable(T_new,output_file_name);
% check
disp(readtable(output_file_name,'VariableNamingRule','preserve'))
Time [min] Event Name 1A: O2 concentration [µM] 1B: O2 concentration [µM] 1A: Amp raw [V] 1B: Amp raw [V] __________ _______________ _________________________ _________________________ _______________ _______________ 0.2 {'Baseline' } 171.37 171.83 0.3361 0.319 1.2 {0×0 char } 168.17 168.59 0.3532 0.3354 1.27 {'Succinate' } 166.93 168.38 0.3555 0.336 2.27 {0×0 char } 153.07 154.47 0.5407 0.5002 3.27 {0×0 char } 132.27 133.79 0.7163 0.6932 4.27 {0×0 char } 110.25 112.02 0.8575 0.8485 5.27 {0×0 char } 87.513 89.532 0.9673 0.9683 6.27 {0×0 char } 64.453 66.747 1.0489 1.0566 7.27 {0×0 char } 41.259 43.919 1.1042 1.1182 8.27 {0×0 char } 18.473 21.508 1.1362 1.1542 9.27 {0×0 char } -0.8695 0.9792 1.1045 1.1582 10.27 {0×0 char } -0.9583 0.2044 0.8311 0.8718 11.27 {0×0 char } -1.0381 0.1703 0.631 0.667 12.27 {0×0 char } -0.9671 0.1447 0.4831 0.517 13.27 {0×0 char } -0.9938 0.1362 0.3772 0.409 14.27 {0×0 char } -0.976 0.1192 0.3033 0.3294 15.27 {0×0 char } -1.0026 0.1022 0.2516 0.2721 16.27 {0×0 char } -1.0204 0.1022 0.215 0.2298 17.27 {0×0 char } -1.047 0.0937 0.1882 0.1993 18.27 {0×0 char } -1.0115 0.0851 0.1688 0.1771 19.27 {0×0 char } -1.0293 0.0766 0.1541 0.1591 20.27 {0×0 char } -1.0293 0.0766 0.1438 0.1444 20.43 {'Reperfusion'} -0.3283 0.1533 0.5722 0.3142 21.43 {0×0 char } 90.947 90.63 1.2374 1.3028 22.43 {0×0 char } 128.91 128.49 1.3692 1.4404 23.43 {0×0 char } 143.55 142.94 1.5093 1.591 24.43 {0×0 char } 148.88 148.13 1.6413 1.7382 25.43 {0×0 char } 150.55 149.74 1.7618 1.8812 26.43 {0×0 char } 150.91 150.06 1.885 2.0125 27.43 {0×0 char } 150.78 149.95 1.9944 2.1324 28.43 {0×0 char } 150.49 149.76 2.0996 2.2512 29.43 {0×0 char } 150.15 149.53 2.207 2.3573 30.43 {0×0 char } 149.86 149.36 2.3019 2.4576 31.43 {0×0 char } 149.6 149.15 2.3962 2.5662 32.43 {0×0 char } 149.32 149.03 2.488 2.662 33.43 {0×0 char } 149.11 148.9 2.5808 2.7527 34.43 {0×0 char } 148.98 148.82 2.6622 2.8459 35.43 {0×0 char } 148.79 148.69 2.7424 2.9272 36.43 {0×0 char } 148.74 148.69 2.8208 3.014 37.43 {0×0 char } 148.61 148.65 2.8992 3.0925 38.43 {0×0 char } 148.49 148.65 2.9762 3.1725 39.43 {0×0 char } 148.45 148.64 3.0413 3.251 40.43 {0×0 char } 148.45 148.62 3.1064 3.3253 41.43 {0×0 char } 148.52 148.66 3.1723 3.3985
  11 Comments
Voss
Voss on 3 Aug 2023
This line:
times = all_times(event_rows(ii)):all_times(event_rows(ii+1));
generates a vector with spacing 1 from the current event time (all_times(event_rows(ii))) to at or near (but not exceeding) the next event time (all_times(event_rows(ii+1))).
To change the spacing from 1 to something else, you can do:
spacing = 3; % every 3 minutes instead of every 1 minute
times = all_times(event_rows(ii)):spacing:all_times(event_rows(ii+1));
But you better be sure that the times generated exist in the file, at least within the tolerance used in the next line:
[~,idx] = ismembertol(times,all_times,1e-4);
I'm not sure what changing the spacing has to do with averaging the first 5 samples after each 'h' event in the calibration file. Maybe you should ask a new question about that, becuase it seems quite different than the original question here.
Annabel Sorby-Adams
Annabel Sorby-Adams on 3 Aug 2023
Thank you so much this is great to know how to change the time spacing. I'll submit a seperate question re averaging.
Thanks again!

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!