Load and Modify Data
This example shows how to load and modify data using Model-Based Calibration Toolbox™ command-line interface. Data can be loaded from files (Excel® files, MATLAB® files, text files) and from the MATLAB® workspace. You can define new variables, apply filters to remove unwanted data, and apply test notes to filtered tests.
Load Data from Excel
Load data from holliday.xlsx.
dataFile = fullfile( matlabroot, 'toolbox',... 'mbc', 'mbctraining', 'holliday_data.mat' ); data = mbcmodel.CreateData( dataFile ); get( data )
Name: 'holliday_data'
NumRecords: 270
NumSignals: 7
NumTests: 27
RecordsPerTest: [10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10]
IsEditable: 1
IsBeingEdited: 0
Owner: []
SignalNames: [7×1 string]
SignalUnits: [7×1 string]
Filters: [0×0 struct]
TestFilters: [0×0 struct]
UserVariables: [0×0 struct]
data.SignalNames
ans = 7×1 string
"afr"
"egr"
"load"
"n"
"spark"
"logno"
"tq"
Plot Data
You can use the SignalName as an input to the Value method. Plot the first 5 tests.
x = zeros(10,5); y = zeros(10,5); name = cell(1,5); % Collect the data as columns to pass to plot. for tn = 1:5 x(:,tn) = data.Value( 'spark', tn ); y(:,tn) = data.Value( 'tq', tn ); name{tn} = sprintf( 'Test %d', tn ); end plot( x, y, 'x-' ); legend( name ); grid on xlabel( sprintf( '%s [%s]', 'spark', data.SignalUnits{5} ) ); ylabel( sprintf( '%s [%s]', 'tq', data.SignalUnits{5} ) ); title( 'tq vs. spark' );
![Figure contains an axes object. The axes object with title tq vs. spark, xlabel spark [deg], ylabel tq [deg] contains 5 objects of type line. These objects represent Test 1, Test 2, Test 3, Test 4, Test 5.](../../examples/mbc/win64/LoadingAndModifyingDataExample_01.png)
Remove Outliers and Problem Tests
Add a filter to keep tests where the mean torque is greater than 10. A filter is a constraint on the data set you can use to exclude some tests (test filter) or records (filter). You must call BeginEdit before making changes. The data is only updated when you call CommitEdit.
numberOfTestsBefore = data.NumberOfTests; data.BeginEdit; data.AddTestFilter( 'mean(tq)>10' ); data.CommitEdit; numberOfTestsAfter = data.NumberOfTests; fprintf( 'Removed %d tests.\n', numberOfTestsBefore-numberOfTestsAfter );
Removed 9 tests.
Add New Variable
You can add new variables to the data set.
data.BeginEdit;
data.AddVariable( 'POWER=tq*n' );
data.CommitEdit;
signalNamesBefore = data.SignalNamessignalNamesBefore = 8×1 string
"afr"
"egr"
"load"
"n"
"spark"
"logno"
"tq"
"POWER"
% POWER is now in the list of SignalNames, and can be used to define other % new variables. data.BeginEdit; data.AddVariable( 'POWER_SQUARED=POWER^2' ); data.CommitEdit; signalNamesAfter = data.SignalNames
signalNamesAfter = 9×1 string
"afr"
"egr"
"load"
"n"
"spark"
"logno"
"tq"
"POWER"
"POWER_SQUARED"
Apply a Filter
Add a filter to keep only records where speed is greater than 1000.
numberOfRecordsBefore = data.NumberOfRecords; data.BeginEdit; data.AddFilter( 'n>1000' ); data.CommitEdit; numberOfRecordsAfter = data.NumberOfRecords; fprintf( 'Removed %d records.\n', numberOfRecordsBefore-numberOfRecordsAfter);
Removed 38 records.