how to do logical indexing for, for and if loops to create a new dataset every time the condition is met.

11 views (last 30 days)
Hi, i am trying to learn the loops in matlab and stuck in a situtaion. I have a time series dataset and i would like to do logical indexing of the table based on variable called global solar irradiance. Every time the value is between 180 and 400, the loops should create a new table based on whenever the condition is met. This would mean i should end up with around 30 to 40 or even more subsets/ tables . I have no idea where to begin. I know i would require a for and if loops for this .
I tried using simple logical indexing like
% refmean= 728
lower_threshold= 180 %0.01 * refmean
upper_threshold = 400 %0.80* refmean
highligting_indices=find(T3_filtered_final.("Global Solar Irradiance")>=lower_threshold & T3_filtered_final.("Global Solar Irradiance")<=upper_threshold)
New_Table=T3_filtered_final(highligting_indices,:)
This new table will have all the time intervals where the value of global solar irradiance meets the condition, but i would like to make subsets that whenever the conditions are met , the program should output a new table where the first value is always something near to 180 and last value near to 400. The new tables could have 1 second time gap or maybe 10 minutes time gap.
How to proceed with this program
for i =1:height(T3_filtered_final)% loop through whole table
if T3_filtered_final.("Global Solar Irradiance")(i)>=180 && T3_filtered_final.("Global Solar Irradiance")(i)<=400
% how to proceed
end
end
sample data is provided within.
  9 Comments
arjun luther
arjun luther on 27 Oct 2023
5-6 has time gaps before and after, while annotating the graph i missed a point between 6-7, which would have started a little after 6. I opted for a little different approach for this whole scenario as i was not getting closer to the solution. I already had a program that would highlight me the dips. Usually between each dip there is a time gap of arbitrary length but more than 5 seconds. I made a logical indexing to find the indices of those time gaps and using these time gap indices, i created my new subsets ,i.e, using adjacent indices. So far the tables look fine to me with this aproach.

Sign in to comment.

Accepted Answer

SAI SRUJAN
SAI SRUJAN on 30 Oct 2023
Hi Arjun luther,
I understand that you have a time series dataset in MATLAB and you want to create tables based on a condition involving the "Global Solar Irradiance" variable. Specifically, you want to create new tables whenever the value of "Global Solar Irradiance" is between 180 and 400.
You can follow the given example to proceed further.
% Set the lower and upper thresholds
lower_threshold = 180;
upper_threshold = 400;
% Initialize variables
subsetTables = {}; % Cell array to store subset tables
startIndex = 1; % Index to track the start of each subset - starting index
for i = 1:height(T3_filtered_final)
if T3_filtered_final.("Global Solar Irradiance")(i) >= lower_threshold && T3_filtered_final.("Global Solar Irradiance")(i) <= upper_threshold
% If the condition is met, update the start index
if startIndex == 0
startIndex = i;
end
else
% If the condition is not met and a subset has started,
% create a new table and insert it.
if startIndex ~= 0
endIndex = i - 1;
subsetTables{end+1} = T3_filtered_final(startIndex:endIndex, :);
startIndex = 0; % Reset the start index
end
end
end
% Check if a subset is still in progress at the end of the loop
if startIndex ~= 0
endIndex = height(T3_filtered_final);
subsetTables{end+1} = T3_filtered_final(startIndex:endIndex, :);
end
% You will end up with a cell array of tables
% which are created whenever conditon is met
% Access each subset table using subsetTables{index}
You can refer to the following documentation to understand more about "table" in MATLAB,

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!