HOW TO SEPARATE NUMERICAL DATA FROM A TEXTFILE?

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 个)

Use textscan and specify header lines.

1 个评论

Pls go through my code. I have already given textscan.It is a large file so giving textcan for 503 rows is not feasible.

请先登录,再进行评论。

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 个评论

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.
Well good for you! Then you can use unique and then my suggested solution, good luck!
HTH

请先登录,再进行评论。

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 个评论

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
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"

请先登录,再进行评论。

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 个评论

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!

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

提问:

2019-1-22

评论:

2019-1-23

Community Treasure Hunt

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

Start Hunting!

Translated by