segregate table in mat lab

1 view (last 30 days)
wissam abdallah
wissam abdallah on 15 Feb 2018
Commented: wissam abdallah on 19 Feb 2018
hi I have the following table in mat lab.
I want to create a procedure or function in mat lab so as I can loop through the mention table and segregate it into two tables:
First one contains all information for the station LR as shown below
Secon one contain all information for the staionn LZ as shown below
Anyone can help me to do this algorithm will be appreciate
Regards.
  6 Comments
Steven Lord
Steven Lord on 16 Feb 2018
Once you've subdivided your data based on ST_CODE, what types of operations do you want to perform on the data? Consider using some of the grouped calculations functionality shown on this documentation page.
I also see you have date data (YEAR, MONTH, DAY) in your table. If you have access to release R2016b you might want to investigate storing your data in a timetable (introduced in R2016b) instead if you want to perform some date-based analysis on your data.
wissam abdallah
wissam abdallah on 16 Feb 2018
after subdivided the global matrix i would like to:
1-plot the mean of TMIN per monthes(12 monthes)in year 2010 and 2011 for station LR40102.
as well as
2- plot the mean of TMIN per monthes(12 monthes)in year 2010 and 2011 for station LZ40103.
3- i would like to separate the Matrix rows of my data into two matrix's one for LR40102 and the other for LZ40103. and thus i could make all the calculation on the appropriate matrix separately.
Regards.

Sign in to comment.

Accepted Answer

Peter Perkins
Peter Perkins on 17 Feb 2018
Simple solution posted in reply to your identical double post.

More Answers (1)

Pawel Jastrzebski
Pawel Jastrzebski on 16 Feb 2018
Edited: Pawel Jastrzebski on 16 Feb 2018
Consider the following code:
% table
t = array2table(rand(10,2))
rowNames = {'a1' 'a2' 'a3' 'a4' 'a5' 'b1' 'b2' 'b3' 'b4' 'b5'}
t.Properties.RowNames = rowNames
% 1st WAY
% if you know where your elements are in the table
% i.e. the split is 50/50 then
splitVector_1stHalf = 1:0.5*size(t,1)
splitVector_2stHalf = 0.5*size(t,1)+1:size(t,1)
t1 = t(splitVector_1stHalf,:)
t2 = t(splitVector_2stHalf,:)
clear t;
%---------------------------------------------------
% table
t = array2table(rand(10,2))
rowNames = {'a1' 'b2' 'b3' 'a4' 'a5' 'b1' 'a2' 'a3' 'b4' 'b5'}
t.Properties.RowNames = rowNames
% 2nd way
% get the split by the row name
% get the row names
names = t.Properties.RowNames
compare = strfind(names,'a');
logicalVecotor = logical(zeros(1,size(compare,1)));
for i=1:size(compare,1)
logicalVecotor(i) = isempty(compare{i});
end
logicalVecotor
t1new = t(logicalVecotor,:)
t2new = t(~logicalVecotor,:)
  3 Comments
Pawel Jastrzebski
Pawel Jastrzebski on 19 Feb 2018
  1. It would have been so much easier if to understand the problem if you had embedded the code as code, not as a plain text.
  2. You're using readtable to import the data from the excel spreadsheet to matlab - you import is as a TABLE already - don't use array2mat after that. I did it in my example as I needed to quickly create some exemplary table to work with.
  3. In my example I make have assigned the row names to the table and made a division using those names. If you want to make sure this method works, you need to import your data with the first column being treated as the 'row names'.
  4. If you want to keep your first column as a variable rather than 'row names' - then I believe Peter Perkins has provided the way to do it in your other thread.
  5. General remark, if someone provides a workable example as a solution to your problem - first thing you should do is to make sure you understand every single function used. That means reading the definitions up in the documentation.
wissam abdallah
wissam abdallah on 19 Feb 2018
Ok . Mr thank you for your support. Regards

Sign in to comment.

Categories

Find more on Tables 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!