Multiple table input issues
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Pesach Nestlebaum
am 4 Mai 2022
Kommentiert: Voss
am 5 Mai 2022
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
0 Kommentare
Akzeptierte Antwort
Voss
am 5 Mai 2022
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);
lightlevels{1}
lightlevels{2}
2 Kommentare
Weitere Antworten (1)
Geoff Hayes
am 5 Mai 2022
@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
Siehe auch
Kategorien
Mehr zu Biological and Health Sciences finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!