Clear Filters
Clear Filters

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

9 views (last 30 days)
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 Comment
Stephen23
Stephen23 on 20 Aug 2024 at 11:06
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

Sign in to comment.

Answers (1)

Arjun
Arjun on 20 Aug 2024 at 10:48
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!

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!