Multiple table input issues

조회 수: 4 (최근 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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by