Multiple table input issues

1 次查看(过去 30 天)
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

更多回答(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
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 CenterFile Exchange 中查找有关 Biological 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