Locate values in a matrix

I have multiple relatively large dataset.
each dataset represents loads on a ship hull, over a 24 hour period.
So the dataset is in matrix format, 17250922x11 table.
The first column is set to time, and the following 10 columns are values from different sensors on the ship hull.
My goal is to:
1) Graphically produce a readable and visually pleasing graph (or several) for when there are peak loads (whne the ship comes into contact with ice). I have managed to produce a graph, but it is very noisy and very compact. Maybe I should filter it?
2) Locate and display these peaks, as there are several of them in each column and each dataset. The values during these peaks will offcourse vary from the rest of the values due to heavy loads.
Anyway I am very new to MATlab so I struggle alot with this and will be grateful for any help.
Kindly
Kris

2 Comments

Ive used this:
as a graph code:
% Load your MATLAB file containing the table
load('load_port_Jan25'); % Replace 'strain_20220125.mat' with the actual file name
% Convert datetime variables to numeric values
time = datenum(load_port_Jan25.LocalTime);
% Repeat the above line for other datetime variables in the table
% Convert the table to a numerical array
data = table2array(load_port_Jan25(:, 2:end)); % Exclude the datetime columns from conversion
% Specify the segment of data to plot (10000 equals about an hour)
segment_start = 122000;
segment_end = 129000;
% Select the column to plot (replace '1' with the desired column index)
column_index = 5;
column_data = data(:, column_index);
% Plot the selected column over time
figure;
plot(time(segment_start:segment_end), column_data(segment_start:segment_end));
selected_data = data(segment_start:segment_end, column_index);
% Calculate the mean of the selected data
mean_value = mean(selected_data);
% Fit a polynomial trend line to the data
degree = 3; % Degree of the polynomial for the trend line
coefficients = polyfit(1:numel(selected_data), selected_data, degree);
trend_line = polyval(coefficients, 1:numel(selected_data));
% Plot the selected data, mean value, and trend line
figure;
plot(time(segment_start:segment_end), selected_data);
hold on;
line([time(segment_start), time(segment_end)], [mean_value, mean_value], 'Color', 'red');
plot(time(segment_start:segment_end), trend_line, 'Color', 'black', 'LineStyle', '--');
hold off;
xlabel('Local-Time');
ylabel('Load');
title('Load with Mean Value and Trend Line');
legend('Load', 'Mean Value', 'Trend Line');
% Format the time axis as minutes and seconds
datetick('x', 'MM:SS', 'keepticks');
% Add grid
grid on;

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 11 Jul 2023
Edited: Star Strider on 11 Jul 2023
A reasonably general approach would be to use the Signal Processing Toolbox sgolayfilt function to smooth the data (there are oither ways as well, for example smoothdata) and then to locate the peaks, use the islocalmax function or the Signal ProcessingToolbox findpeaks function.
EDIT — (11 Jul 2023 at 16:43)
It would hellp to have the data, and a description of what you want to do with it.
I encourage you to use the datetime function instead of datenum to do date calculations, plotting, and other processing.
EDIT — (11 Jul 2023 at 17:41)
I understand about the data, however I cannot do anything specific without at least a sample of it. What I have already posted is essentially the best I can do.

5 Comments

Kristoffer
Kristoffer on 11 Jul 2023
Moved: Voss on 11 Jul 2023
Hi Star Strider!
I have attached a screenshot and an attempt at coding above.
The file is 1.6 gb so i dont think i can attach that XD
The goal is
1) Graphically produce a readable and visually pleasing graph (or several) for when there are peak loads (whne the ship comes into contact with ice). I have managed to produce a graph, but it is very noisy and very compact. Maybe I should filter it?
2) Locate and display these peaks, as there are several of them in each column and each dataset. The values during these peaks will offcourse vary from the rest of the values due to heavy loads.
Thanks for quick response
Yeah ok, that is no problem,
So this is the first 200 rows of the marix.
I am not certain how to define ‘peak load’ so here are some guesses —
% type('sample.txt')
% opts = detectImportOptions('sample.txt')
% v1 = getvaropts(opts,'Var1')
% opts = setvartype(opts,'Var1','datetime')
% opts = setvartype(opts,'Var2','datetime')
% opts = setvaropts(opts,'Var1','InputFormat','dd-MMM-uuuu')
% opts = setvartype(opts, 'Var2','datetime')
% opts = setvaropts(opts,'Var2','InputFormat','HH:mm:ss.SSSS')
T1 = readtable('sample.txt');
Date = datetime(T1.Var1, 'InputFormat','''''dd-MMM-uuuu');
Time = datetime(T1.Var2, 'InputFormat','HH:mm:ss.SSSS''''');
DateTime = Date + timeofday(Time);
DateTime.Format = 'dd-MMM-yyyy HH:mm:ss.SSSS';
T1 = addvars(T1, DateTime,'Before','Var1');
T1 = removevars(T1,{'Var1','Var2'});
T1 = renamevars(T1, compose('Var%d',3:12), compose('Var%d',1:10))
T1 = 201×11 table
DateTime Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 _________________________ __________ __________ __________ __________ __________ __________ __________ __________ __________ __________ 25-Jan-2022 00:00:00.0038 4.1987e+05 1.5439e+05 8.8587e+05 2.5323e+05 1.7252e+06 85799 44943 85826 1.0972e+06 5.5946e+05 25-Jan-2022 00:00:00.0087 3.5796e+05 1.6542e+05 9.2113e+05 1.367e+05 1.4236e+06 1.1076e+05 28950 76087 8.4453e+05 4.1109e+05 25-Jan-2022 00:00:00.0138 3.1219e+05 1.193e+05 8.6067e+05 48685 1.4414e+06 21096 84124 1.009e+05 9.2134e+05 3.6272e+05 25-Jan-2022 00:00:00.0188 5.1281e+05 1.1469e+05 8.8922e+05 2.1386e+05 1.4327e+06 95920 57869 94396 9.6186e+05 5.317e+05 25-Jan-2022 00:00:00.0238 1.038e+05 1.7119e+05 8.6182e+05 66011 1.449e+06 67302 80654 1.3979e+05 8.1016e+05 3.0325e+05 25-Jan-2022 00:00:00.0288 1.3666e+05 19671 8.0107e+05 89864 1.596e+06 37581 1.2443e+05 1.5011e+05 9.5952e+05 3.5365e+05 25-Jan-2022 00:00:00.0337 2.253e+05 1.0461e+05 8.2734e+05 16687 1.5106e+06 10184 97709 1.2982e+05 9.9411e+05 4.7996e+05 25-Jan-2022 00:00:00.0388 92914 58241 8.1808e+05 31818 1.681e+06 55542 1.1528e+05 1.5414e+05 1.0628e+06 3.3231e+05 25-Jan-2022 00:00:00.0438 3.5433e+05 15277 8.8776e+05 1.8613e+05 1.6029e+06 5554.3 83371 1.1234e+05 1.0489e+06 4.2189e+05 25-Jan-2022 00:00:00.0488 2.4607e+05 98160 9.0816e+05 1.667e+05 1.5626e+06 89498 44351 1.3386e+05 1.0223e+06 4.617e+05 25-Jan-2022 00:00:00.0538 2.1898e+05 14375 8.7599e+05 45511 1.5619e+06 97851 59187 1.0471e+05 9.8334e+05 4.0128e+05 25-Jan-2022 00:00:00.0588 2.3362e+05 15277 8.3292e+05 327.49 1.6479e+06 66372 88684 84928 9.9295e+05 4.0673e+05 25-Jan-2022 00:00:00.0638 1.0916e+05 18018 8.7329e+05 28750 1.7372e+06 60137 65764 1.8644e+05 1.0526e+06 3.9118e+05 25-Jan-2022 00:00:00.0688 2.0267e+05 72423 9.5165e+05 62907 1.5133e+06 1.7589e+05 6353.3 1.2236e+05 8.6396e+05 3.7406e+05 25-Jan-2022 00:00:00.0738 2490.2 1.1497e+05 8.9501e+05 85873 1.4783e+06 1.8591e+05 46274 1.4474e+05 8.1227e+05 3.187e+05 25-Jan-2022 00:00:00.0789 1.1472e+05 1.9408e+05 9.0444e+05 38709 1.3952e+06 2.3144e+05 45185 1.4925e+05 8.3448e+05 3.9587e+05
figure
plot(T1{:,1}, T1{:,2:end})
grid
title('Original')
VarsSGfilt = sgolayfilt(T1{:,2:end}, 3, 11); % Filter Noise
for k = 1:size(VarsSGfilt,2)
SEM = std(VarsSGfilt(:,k))/sqrt(numel(VarsSGfilt(:,k))); % Standard Error Of The MEan
VarCI95 = SEM*1.96 + mean(VarsSGfilt(:,k)); % +95% Confidence Interval For Data Set
[pks,locs] = findpeaks(VarsSGfilt(:,k), 'MinPeakProminence',VarCI95); % Detect & Store Peaks & Indices
Peaks{k} = pks;
Locs{k} = locs;
end
figure
plot(T1.DateTime, VarsSGfilt)
hold on
for k = 1:size(VarsSGfilt,2)
plot(T1.DateTime(Locs{k}), Peaks{k}, 'vr')
end
hold off
grid
title('Filtered')
figure
tiledlayout(5,2)
for k = 1:size(VarsSGfilt,2)
nexttile
plot(T1.DateTime, VarsSGfilt(:,k))
hold on
plot(T1.DateTime(Locs{k}), Peaks{k}, 'vr')
hold off
grid
title(sprintf('Var%d',k))
end
sgtitle('Filtered Plot Array')
This first filters the variables, and then uses the 95% confidence interval on the mean of each data set with 'MinPeakProminence' to define the peaks, since this varies with each signal. There are likely several ways to do this, and since this is quite outisde my areas of expertise, I defer to you for that. I used tiledlayout for the plot array. It has some advantages with respect to programming, however it also has limitations with respect to the individual axes. It you want more control over the axes, use subplot instead. The code is quite similar.
I had significant problems with the datetime variable, and finally gave up on importing it as a single variable with the options structure. It was difficult enough to import it and convert it as is.
I will help to refine this as desired.
EDIT — (12 Jul 2023 at 03:19)
Expanded explanation of the statistical calculation used to define the 'MinPeakProminence' value.
.
Hey!
This is great.
Really appreciate the effort man, this is for a thesis and I was getting pretty lost in the coding of this.
So am I correct that I replace sample.txt with the actual matlab file name?
And Var1, Var2 and so on is replaced with the column name?
Thank you!
Yes to both.
The column (variable) names should import automatically, and should appear in the table. (They were not part of ‘sample.txt’.)
Change ‘DateTime’ to ‘LocalTime’ wherever it appears, for consistency with the original file.
To get the variable names from the table, after you import it, create this variable:
VN = T1.Properties.VariableNames;
It is a cell array of the variable names, so you can then simply refer to them as ‘VN{1}’, ‘VN{2}’ and so forth, just as any other cell array. In the tileldlayout for loop the title call would then be:
title(VN{k})
You may need to experiment with it to get it to work as you want it to.
When you get the file imported as a table, one option to follow up with it here would be to use the writetable function to write perhaps the first 500 rows of it to a file, and then post that here as a follow-up to ‘sample.txt’.
For example:
writetable(T1(1:500,:), 'sampletable.txt')
That would make it easier for me to hellp you with it, if necessary.
.

Sign in to comment.

Categories

Products

Release

R2021a

Asked:

on 11 Jul 2023

Commented:

on 12 Jul 2023

Community Treasure Hunt

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

Start Hunting!