HOW TO SEPARATE NUMERICAL DATA FROM A TEXTFILE?

조회 수: 2 (최근 30일)
Lakshmi P
Lakshmi P 2019년 1월 22일
댓글: Lakshmi P 2019년 1월 23일
I have to find the median of height ( indicated by the 2nd from last column in the data file i have attached below), at each levels seperately. I have already written a code to find the median which am attaching below. But now i have to seperate them according to their levels ( Levels are indicated by the first column in the data file, which goes like 1,2,3,1,2,3.....) Reading starts from 16th line onwards. The median is found out in such a manner that if the files are placed side by side, we get 4 height datas from a single line/level...like that.....THe result i got from my code looks like this: MEDIAN OF HEIGHT : 500,100,50, 448.05,95.2...In this 500 indicates the median from level1, 100 from level2, 50 from level3. 448.05 from level1, 95.2 from level2.... so on.....IT GOES ON LIKE THIS
So can u please help me modify my code so that i can seperate these median according to their levels. So that output looks likethis : MEDIAN OF HEIGHT AT LEVEL1 :500,448.05...MEDIAN AT LEVEL2: 100,95.2....MEDIAN AT LEVEL3: 50,...
I have tried various methods and none of it works. Iam new to matlab and have very limited programming skills. Pls help

답변 (4개)

KSSV
KSSV 2019년 1월 22일
Use textscan and specify header lines.
  댓글 수: 1
Lakshmi P
Lakshmi P 2019년 1월 22일
편집: Lakshmi P 2019년 1월 22일
Pls go through my code. I have already given textscan.It is a large file so giving textcan for 503 rows is not feasible.

댓글을 달려면 로그인하십시오.


Bjorn Gustavsson
Bjorn Gustavsson 2019년 1월 22일
Well you need to find out how many levels you have in your height-arrays, for that I suggest you take a look at the function unique:
helpwin unique
Then you need to determine if you have discrete enough levels to use that function (measurement noise might give you levels varying in a "physically insignifficant" way - this would give you a uselessly large number of levels). If that's the case you'll have to loop over the unique levels and do something like:
for i_levels = numlevels:-1:1,
m_level(i_levels) = mean(variable_of_interest(height==level(i_levels)));
end
If you have measurement noise in your levels then I suggest you look at the file-exchange contribution consolidator
HTH
  댓글 수: 2
Lakshmi P
Lakshmi P 2019년 1월 22일
편집: Lakshmi P 2019년 1월 22일
The total number of rows is 507. The levels are the height at which measurements are taken. It goes like 1,2,3,1,2,3,1,2,3... so on. I have to seperate my medians under level1 level2 and level3.
Bjorn Gustavsson
Bjorn Gustavsson 2019년 1월 22일
Well good for you! Then you can use unique and then my suggested solution, good luck!
HTH

댓글을 달려면 로그인하십시오.


Jeff Miller
Jeff Miller 2019년 1월 22일
mydata = readtable('1Oct6AM.txt','Headerlines',16);
mymedians = CondMedians(mydata,'Var13','Var1')
gives
mymedians =
Var1 Var13_median
____ ____________
1 974.3
2 1005.6
3 1005.4
The function CondMedians is in RawRT.
  댓글 수: 2
lakshmi dwaraka
lakshmi dwaraka 2019년 1월 23일
편집: lakshmi dwaraka 2019년 1월 23일
Thankyou for replaying! But it doesnt solve my question. I have already found out the median. My code is attached there. What i want to do is seperate my median according to the column1 (looks like 1,2,3,1,2,3...)of the file. so that result looks like : median at level1 is 500, 448.05...median at level2 is 100,99.2... median at level3..so on.. These 3 should be 3 seperate files . Hope you understood. Thanks again
Jeff Miller
Jeff Miller 2019년 1월 23일
No, I don't understand. The table mymedians in my answer shows the medians for the 13th column separately for each of the levels 1, 2, and 3 of Var1. Isn't that what you want? I do not understand about "3 separate files"

댓글을 달려면 로그인하십시오.


Jeff Miller
Jeff Miller 2019년 1월 23일
Sorry, I misunderstood completely. Maybe this will do what you want:
level1 = mod(1:507,3)==1;
level1_medians = median_height(level1);
level2 = mod(1:507,3)==2;
level2_medians = median_height(level2);
level3 = mod(1:507,3)==3;
level3_medians = median_height(level3);
  댓글 수: 1
Lakshmi P
Lakshmi P 2019년 1월 23일
Thankyou for replaying! But the answer seems to be wrong.
For the level1 median should be 500, 448.05, 398.7... so on
But the answer according to your code for level1 is:
Screenshot from 2019-01-23 12-54-26.png
I have to find the median. I think there is something wrong with calculation.
Thankyou!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by