nested loops to find values then execute commands
I have written a script to find the maximum and minimum values of 4 time series and then truncate each time series to these values as they are all of different length.
I want it to record the maximum and minimum values of all the time series first (lines 15-19), then after that store the overall smallest and largest values to new variables (lines 20-23).
At the moment, its constantly updating variables as it loops through, so its not using the overall smallest/largest values to truncate the time series.
I have tried putting lines 15-19 in another loop so it executes this first, but it doesnt seem to make any difference. Can anybody help?
%import all the csv files in the folder
csvFiles = dir(fullfile(folderPath, ‘*.csv’));
%read in all the csv files
for i = 1:length(csvFiles)
%get the individual file pathway
csvFilePath = fullfile(folderPath, csvFiles(i).name);
%read the file in
data = readmatrix(csvFilePath);
%remove nan’s from the timeseries
data = data(~any(isnan(data), 2), :);
%remove non-unique datapoints from the timeseries
[~, id] = unique(data(:,1)) ;
data = data(id,:);
for a = 1:length(csvFiles)
%find and save the maximum value of all the time series
maxVal(1) = max(data(:,1));
%find and save the minimum value of all the time series
minVal(1) = min(data(:,1));
%find and save the mean dt of all the time series
dt(1) = mean(diff(data(:,1)));
end
%find the time series that ends soonest
smallestMaxValue = min(maxVal)
%find the time series that starts latest
largestMinValue = max(minVal)
%truncate all the time series to the shortest one
truncateRow = find(data(:,1) > smallestMaxValue, 1, ‘first’); %find the index of the first value in the first column when the shortest time series is exceeded
if ~isempty(truncateRow)
truncatedData = data(1:truncateRow-1, :); %if there is a value in truncateRow, then truncate the dataset up to this point
else
truncatedData = data; %if there is no value in truncateRow (e.g. shortest time series), then leave the data as it is
end
endI have written a script to find the maximum and minimum values of 4 time series and then truncate each time series to these values as they are all of different length.
I want it to record the maximum and minimum values of all the time series first (lines 15-19), then after that store the overall smallest and largest values to new variables (lines 20-23).
At the moment, its constantly updating variables as it loops through, so its not using the overall smallest/largest values to truncate the time series.
I have tried putting lines 15-19 in another loop so it executes this first, but it doesnt seem to make any difference. Can anybody help?
%import all the csv files in the folder
csvFiles = dir(fullfile(folderPath, ‘*.csv’));
%read in all the csv files
for i = 1:length(csvFiles)
%get the individual file pathway
csvFilePath = fullfile(folderPath, csvFiles(i).name);
%read the file in
data = readmatrix(csvFilePath);
%remove nan’s from the timeseries
data = data(~any(isnan(data), 2), :);
%remove non-unique datapoints from the timeseries
[~, id] = unique(data(:,1)) ;
data = data(id,:);
for a = 1:length(csvFiles)
%find and save the maximum value of all the time series
maxVal(1) = max(data(:,1));
%find and save the minimum value of all the time series
minVal(1) = min(data(:,1));
%find and save the mean dt of all the time series
dt(1) = mean(diff(data(:,1)));
end
%find the time series that ends soonest
smallestMaxValue = min(maxVal)
%find the time series that starts latest
largestMinValue = max(minVal)
%truncate all the time series to the shortest one
truncateRow = find(data(:,1) > smallestMaxValue, 1, ‘first’); %find the index of the first value in the first column when the shortest time series is exceeded
if ~isempty(truncateRow)
truncatedData = data(1:truncateRow-1, :); %if there is a value in truncateRow, then truncate the dataset up to this point
else
truncatedData = data; %if there is no value in truncateRow (e.g. shortest time series), then leave the data as it is
end
end I have written a script to find the maximum and minimum values of 4 time series and then truncate each time series to these values as they are all of different length.
I want it to record the maximum and minimum values of all the time series first (lines 15-19), then after that store the overall smallest and largest values to new variables (lines 20-23).
At the moment, its constantly updating variables as it loops through, so its not using the overall smallest/largest values to truncate the time series.
I have tried putting lines 15-19 in another loop so it executes this first, but it doesnt seem to make any difference. Can anybody help?
%import all the csv files in the folder
csvFiles = dir(fullfile(folderPath, ‘*.csv’));
%read in all the csv files
for i = 1:length(csvFiles)
%get the individual file pathway
csvFilePath = fullfile(folderPath, csvFiles(i).name);
%read the file in
data = readmatrix(csvFilePath);
%remove nan’s from the timeseries
data = data(~any(isnan(data), 2), :);
%remove non-unique datapoints from the timeseries
[~, id] = unique(data(:,1)) ;
data = data(id,:);
for a = 1:length(csvFiles)
%find and save the maximum value of all the time series
maxVal(1) = max(data(:,1));
%find and save the minimum value of all the time series
minVal(1) = min(data(:,1));
%find and save the mean dt of all the time series
dt(1) = mean(diff(data(:,1)));
end
%find the time series that ends soonest
smallestMaxValue = min(maxVal)
%find the time series that starts latest
largestMinValue = max(minVal)
%truncate all the time series to the shortest one
truncateRow = find(data(:,1) > smallestMaxValue, 1, ‘first’); %find the index of the first value in the first column when the shortest time series is exceeded
if ~isempty(truncateRow)
truncatedData = data(1:truncateRow-1, :); %if there is a value in truncateRow, then truncate the dataset up to this point
else
truncatedData = data; %if there is no value in truncateRow (e.g. shortest time series), then leave the data as it is
end
end for loop, loop, nested loop MATLAB Answers — New Questions