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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!