segregate table in mat lab
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Steven Lord
2018-2-16
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.
采纳的回答
更多回答(1 个)
Pawel Jastrzebski
2018-2-16
编辑:Pawel Jastrzebski
2018-2-16
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 个评论
Pawel Jastrzebski
2018-2-19
- 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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!