segregate table in mat lab
1 view (last 30 days)
Show older comments
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
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.
Accepted Answer
Peter Perkins
on 17 Feb 2018
Simple solution posted in reply to your identical double post.
0 Comments
More Answers (1)
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
on 19 Feb 2018
- 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.
- 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.
- 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'.
- 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.
- 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.
See Also
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!