Extract cell rows to new array after strfind or strcmp matching.

1 次查看(过去 30 天)
Hi everyone, I have an array of data (500*7) and the 7th column is a series of date array, they are all in random order so I would like to rearrange by month_array. So for each cell matching the portion of string '01/2015' i want a loop that extract the all rows (1:7) for example and store it in a new array etc. I have included a picture to ease the understanding.
jan_table = [];
for i=1:size(ship_table);
idx = strfind(ship_table(i,7),'01/2015');
if idx isequal('4')
STORE ROW IN NEW ARRAY jan_table
else
jan_table = [jan_table num2cell(ships_table{i})];
end
end
If anyone could help, thanks!
  1 个评论
Stephen23
Stephen23 2024-8-20
Avoid using loops.
Avoid expanding arrays inside loops.
Avoid using deprecated date functions e.g. DATESTR.
Avoid string comparisons e.g. STRFIND or CONTANS.
Use DATETIME and related methods/functions: https://www.mathworks.com/help/matlab/ref/datetime.html

请先登录,再进行评论。

回答(1 个)

Arjun
Arjun 2024-8-20
Hi,
As per my understanding, you have an array in which there is a date string column, and you want to extract rows corresponding to January 2015 or 01/2015.
I am assuming that your data is stored in a cell array and each entry in the 7th column of the cell array is in the format dd/mm/yyyy.
For demonstration purposes, let's assume you have an array of date strings. Next, define a variable targetMonthYear and assign it the value '01/2015', representing the month and year you are interested in. You can then loop through each element in your sample data, using MATLAB's contains function to check if the current date string includes the targetMonthYear.
Here is a reference code for the same:
% Assuming 'ship_table' is your 500x7 cell array with date strings in the 7th column
% Example setup (replace with your actual data)
ship_table = cell(500, 7);
for i = 1:500
ship_table{i, 7} = datestr(now - randi(365*2), 'dd/mm/yyyy');
% Random dates for demonstration
end
% Define the target month and year
targetMonthYear = '01/2015';
% Initialize an empty cell array to store the filtered rows
jan_table = {};
% Loop through each row of the ship_table
for i = 1:size(ship_table, 1)
% Extract the date string from the 7th column
currentDate = ship_table{i, 7};
% Check if the date string contains the target month and year
if contains(currentDate, targetMonthYear)
% Append the current row to the jan_table array
jan_table = [jan_table; ship_table(i, :)];
end
end
% Display the results
disp('Extracted Rows for January 2015:');
disp(jan_table);
Kindly refer to the below MathWorks documentation link to know more about the contains function used for string matching: https://www.mathworks.com/help/matlab/ref/string.contains.html?s_tid=srchtitle_site_search_1_contains
I hope this helps!

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by