How to write a loop for this case?

Hello everyone,
I have a big table (25560 x 12) which includes data for 71 stations on a monthly bases from 30 years. I want to extract data from it based on the names (from station_name column) and month names (from date column). I want to save all results on the workspace as separate tables.
I searched a lot and write this code that does what I want for 2 stations but unfortunately, I should write it for all 71 stations (change names by hand) that is so time-consuming process. I want to ask if anyone could help me to do it using for loop or something.
%Read one of stations
Ahvaz_table = stations(stations.station_name == "Ahvaz", :);
%extract it month by month with name of month infront of station name:
Ahvaz_Jan = Ahvaz_table(month(Ahvaz_table.date) == 1, :)
Ahvaz_Feb = Ahvaz_table(month(Ahvaz_table.date) == 2, :)
Ahvaz_Mar = Ahvaz_table(month(Ahvaz_table.date) == 3, :)
Ahvaz_Apr = Ahvaz_table(month(Ahvaz_table.date) == 4, :)
Ahvaz_May = Ahvaz_table(month(Ahvaz_table.date) == 5, :)
Ahvaz_Jun = Ahvaz_table(month(Ahvaz_table.date) == 6, :)
Ahvaz_Jul = Ahvaz_table(month(Ahvaz_table.date) == 7, :)
Ahvaz_Aug = Ahvaz_table(month(Ahvaz_table.date) == 8, :)
Ahvaz_Sep = Ahvaz_table(month(Ahvaz_table.date) == 9, :)
Ahvaz_Oct = Ahvaz_table(month(Ahvaz_table.date) == 10, :)
Ahvaz_Nov = Ahvaz_table(month(Ahvaz_table.date) == 11, :)
Ahvaz_Dec = Ahvaz_table(month(Ahvaz_table.date) == 12, :)
%go to next station
Fasa_table = stations(stations.station_name == "Fasa", :);
%extract it month by month with name of month infront of station name:
Fasa_Jan = Fasa_table(month(Fasa_table.date) == 1, :)
Fasa_Feb = Fasa_table(month(Fasa_table.date) == 2, :)
Fasa_Mar = Fasa_table(month(Fasa_table.date) == 3, :)
Fasa_Apr = Fasa_table(month(Fasa_table.date) == 4, :)
Fasa_May = Fasa_table(month(Fasa_table.date) == 5, :)
Fasa_Jun = Fasa_table(month(Fasa_table.date) == 6, :)
Fasa_Jul = Fasa_table(month(Fasa_table.date) == 7, :)
Fasa_Aug = Fasa_table(month(Fasa_table.date) == 8, :)
Fasa_Sep = Fasa_table(month(Fasa_table.date) == 9, :)
Fasa_Oct = Fasa_table(month(Fasa_table.date) == 10, :)
Fasa_Nov = Fasa_table(month(Fasa_table.date) == 11, :)
Fasa_Dec = Fasa_table(month(Fasa_table.date) == 12, :)
% go to next station
.
.
.
Thank you all.
I attached my whole table.

5 件のコメント

Adam Danz
Adam Danz 2020 年 1 月 26 日
This would be very easy if the tables were all stored in a cell array rather than as separate variables. I'm wondering how 71 different tables were created with different variable names. That process can most likely create a cell array of tables rather than 71 different, independent tables.
BN
BN 2020 年 1 月 26 日
Dear Adam Danz,
Thank you for your mentioned about easiest way using cell array. I agree with that. I have all the tables in a cell array, I attached it in this comment namely "C.mat"
Can you please guide me on how to do that? I want to have January, February,...December notwithstanding whatever the years are in all stations in each station, just like the code that provided in my question.
Thank you.
Turlough Hughes
Turlough Hughes 2020 年 1 月 26 日
Just to note that the tables provided in the variable C contain a column entitled data (as opposed date) which seems to be a typo.
Adam Danz
Adam Danz 2020 年 1 月 26 日
If I recall correctly, I believe OP resolved that in a previous question.
Just in case that's still an issue, Behzad Navidi, this loop will rename the "data" column to "date" for all tables in C.
% Change "data" col to "date"
for i = 1:numel(C)
C{i}.Properties.VariableNames = strrep(C{1}.Properties.VariableNames,'data','date');
end
Stephen23
Stephen23 2020 年 1 月 27 日
"I want to save all results on the workspace as separate tables."
And that is the start of the problem.
"... I should write it for all 71 stations (change names by hand) that is so time-consuming process."
Putting meta-data (such as month names) into variable names means that you are doing something wrong.
Repeating code is a sign that you are doing something wrong.
You should be using one array. Then access it in a loop using indexing, dynamic fieldnames, etc.

サインインしてコメントする。

 採用された回答

Adam Danz
Adam Danz 2020 年 1 月 26 日
編集済み: Adam Danz 2020 年 1 月 26 日

1 投票

The variable "C" is a 1xn cell array where each element contains a table. Each table contains a column "date" listing dates in datetime format (from your previous questions).
This solution seaprates each table by month to create an nx12 cell array where Cmo{i,j} contains a subtable from station i and month j.
% Loop through each table and break it apart by month
% creating 12 sub-tables that are stored in the rows
% of Cmo.
Cmo = cell(numel(C),12);
for i = 1:numel(C)
Cmo(i,:) = arrayfun(@(m){C{i}(month(C{i}.date) == m, :)},1:12);
end
Depending on how you'll use these data, it may be more practical to keep the tables all together rather then splitting them by month and using logical indexing to isolate the rows of a table that are associated with a particular month.

その他の回答 (2 件)

woahs
woahs 2020 年 1 月 26 日

1 投票

I'd recommend using structs and dynamic fieldnames instead of setting dynamic variable names for your station names and month. Give this a try and see if it does what you want.
finalStations = struct;
monthText = {'Jan' 'Feb', 'Mar', 'Apr', 'May', 'Jun', ...
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'};
stationNames = unique(stations.station_name);
for sx = 1:length(stationNames)
thisStation = stations(ismember(stations.station_name, stationNames{sx}), :);
adjustedStationName = matlab.lang.makeValidName(stationNames{sx});
for mx = 1:length(monthText)
finalStations.([adjustedStationName, '_', monthText{mx}]) = ...
thisStation(month(thisStation.date) == mx, :);
end
end
Turlough Hughes
Turlough Hughes 2020 年 1 月 26 日
編集済み: Turlough Hughes 2020 年 1 月 26 日

1 投票

I was also taking the route of putting them into a structure with a fieldname for each month.
months = {'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'}
for ii = 1:12
data.(months{c})=cellfun(@(x) x(month(x.data) == ii, :),C,'UniformOutput',false);
end

1 件のコメント

Turlough Hughes
Turlough Hughes 2020 年 1 月 26 日
So data.Jan will have the exact same format as your original cell array but with only data from January of each year.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Type Identification についてさらに検索

製品

リリース

R2018b

タグ

質問済み:

BN
2020 年 1 月 26 日

コメント済み:

2020 年 1 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by