Multiple table input issues

1 回表示 (過去 30 日間)
Pesach Nestlebaum
Pesach Nestlebaum 2022 年 5 月 4 日
コメント済み: Voss 2022 年 5 月 5 日
I have a script that reads multiple tables that contain columns such as "Light", "Temp", "Weight". Each row in these tables represents a "trigger", where the light level, temperature and weight are all recorded.
The script finds which hour contains the greatest number of "triggers" and assigns it to a variable. If you run the script, the variable "intlist" represents the hours with the greatest number of triggers for each table passed through the script.
My issue is, I would like to perform a further action and create an array for the interval where there is the most triggers in each table. I can accomplish this once, for the latest table that the script runs, but I cannot for the life of me get it to work for all the tables. The output I need would be 4 arrays, each listing the lightlevels corresponding the the rows within the interval of highest trigger number.
I believe there is something wrong with the way I structured this loop but I cannot figure out where it is going wrong.
I will attach the script and all 4 tables.
[file_list,path_n] = uigetfile('.xlsx','MultiSelect','on');
if iscell(file_list)==0
file_list=(file_list);
end
for i=1:length(file_list)
filename=file_list{i};
T1 = readtable([path_n filename]);
lidx = T1.Light<=4;
T1.Light(lidx) = (NaN);
Tt = [repmat(fix(now), size(T1,1), 1) + T1.Time];
T1.Time = datetime(Tt, 'ConvertFrom','datenum');
T1.Trigger = ones(size(T1.Time));
TT1 = table2timetable(T1);
TT1c = retime(TT1,'hourly','count');
[maxEvent,idx] = max(TT1c.Trigger);
intlist(i)=idx;
hourlist=hour(T1.Time)==idx;
lightlevels=T1.Light(hourlist);
end

採用された回答

Voss
Voss 2022 年 5 月 5 日
Since lightlevels is a vector, and that vector might be different lengths for different files, it makes sense to store those vectors in a cell array and set one element of that cell array each time through the for loop.
[file_list,path_n] = uigetfile('.xlsx','MultiSelect','on');
if ~iscell(file_list)
file_list = {file_list}; % use { } here, by the way
end
n_files = numel(file_list);
lightlevels = cell(1,n_files); % lightlevels is a cell array now
intlist = zeros(1,n_files); % and intlist is a numeric array (in case you need it)
for i = 1:n_files
T1 = readtable([path_n file_list{i}]);
lidx = T1.Light<=4;
T1.Light(lidx) = NaN;
Tt = repmat(fix(now), size(T1,1), 1) + T1.Time;
T1.Time = datetime(Tt, 'ConvertFrom','datenum');
T1.Trigger = ones(size(T1.Time));
TT1 = table2timetable(T1);
TT1c = retime(TT1,'hourly','count');
[maxEvent,idx] = max(TT1c.Trigger);
intlist(i)=idx; % set the i-th element of intlist
hourlist = hour(T1.Time)==idx;
lightlevels{i} = T1.Light(hourlist); % set the i-th element of lightlevels
end
disp(lightlevels);
{13×1 double} {15×1 double} {12×1 double} {23×1 double}
lightlevels{1}
ans = 13×1
9 10 10 10 10 10 10 10 10 10
lightlevels{2}
ans = 15×1
10 10 10 10 10 10 10 10 10 10
  2 件のコメント
Pesach Nestlebaum
Pesach Nestlebaum 2022 年 5 月 5 日
This works! THANK YOU!!!
Voss
Voss 2022 年 5 月 5 日
You're welcome!

サインインしてコメントする。

その他の回答 (1 件)

Geoff Hayes
Geoff Hayes 2022 年 5 月 5 日
@Peder Axensten - try creating a cell array of the data that you wish to save (I'm assuming that each iteration could produce a different number of lightlevels. For example,
lightlevels = {};
for i=1:length(file_list)
% code from above
hourlist=hour(T1.Time)==idx;
lightlevels{i} = T1.Light(hourlist);
end
  1 件のコメント
Pesach Nestlebaum
Pesach Nestlebaum 2022 年 5 月 5 日
Ok, it created a 1 x 4 cell, but only the 4th column is occupied with a lightlevel list. Column 1 - 3 are empty.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeBiological and Health Sciences についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by