how to do edit table type data and do sliding window in matlab

조회 수: 1 (최근 30일)
SHU HUANG
SHU HUANG 2018년 7월 2일
답변: Naga 2024년 9월 27일
I have imported excel file into matlab and stored it as type of table, the file contains customers' name, number, date of purchase and other such kind of things. now what I to do is to achieve following function in matlab: 1. I can filter specific customer's record by entering his name 2. the showed records of this specific customer are sliding windows, every unit of the sliding windows is like from day1 to day 10 and next is from day5 to day15. so can anyone help me out ? the only hint I know is to use cell type, but I still don't know what is the code look like

답변 (1개)

Naga
Naga 2024년 9월 27일
Hello Huang,
To achieve the functionality you described, you can follow these steps to filter records by a specific customer's name and then display the records in a sliding window format. Below is a sample code that demonstrates how to do this:
% Function to filter the table for records that match the specified customer's name.
function customerRecords = filterCustomerRecords(data, customerName)
customerRecords = data(strcmp(data.CustomerName, customerName), :);
end
% Function sorts the records by date and then displays them in a sliding window format.
% The window size and step size are adjustable.
function displaySlidingWindows(records, windowSize, stepSize)
records = sortrows(records, 'DateOfPurchase'); % Sort by date
numRecords = height(records);
for startIndex = 1:stepSize:(numRecords - windowSize + 1)
endIndex = startIndex + windowSize - 1;
disp(records(startIndex:endIndex, :)); % Display current window
end
end
% Example usage
customerName = 'John Doe';
windowSize = 10;
stepSize = 5;
% Filter and display records
customerRecords = filterCustomerRecords(data, customerName);
displaySlidingWindows(customerRecords, windowSize, stepSize);
Ensure that your table (data) has the correct column names and types as expected by the code. You can adjust the column names in the code to match those in your actual data.

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by