Code for extracting the special values

Hi all,
I have txt file which contains two columns: the first column is time [hourly resolution], and the second column is the answer of my function (values). I want to extract the values from the second column which are based on daily [I mean 0,1,2,3,...]. and then write it in another txt file, which will have two columns and be like: 0 814 1 745 2 660 3 539 . .
How should I have to write my code? Attached my .txt file here.
I appreciate any help and thanks in advance. Hope to hear from you. Sepideh

 採用された回答

Stephen23
Stephen23 2016 年 2 月 2 日
編集済み: Stephen23 2016 年 2 月 2 日

0 投票

% Read original data file:
M = dlmread('remaining particles total.txt','\t');
H = min(M(:,1)):max(M(:,1));
[X,Y] = ismember(H,M(:,1));
assert(all(X),'Some hour values are missing')
% Write new data file:
fid = fopen('newfile.txt','wt');
fprintf(fid,'%d\t%d\n',M(Y,:).');
fclose(fid);
This creates the following new text file:
0 814
1 745
2 660
3 539
4 420
5 355
6 303
... lots here
1821 0
1822 0
1823 0
1824 0
1825 0

4 件のコメント

SEPIDEH
SEPIDEH 2016 年 2 月 2 日
Great,
Thanks a lot for the help. Another tricky question: if I have again another text file: first column showing time [hrly resolution], and the second column the function value. Then if I want to extract every 6hrs, which means 0,6,12,18,24,30,...
Then which part of the code should I modify to have the preferred results? I attached another text file.
Hope to hear from you. Best regards, Sepideh
Stephen23
Stephen23 2016 年 2 月 2 日
You just need to define the variable H to be a vector of time values that you want to keep. You could simply do this:
H = 0:6:max(M(:,1));
If my answer helped you, then please consider accepting it.
SEPIDEH
SEPIDEH 2016 年 2 月 2 日
Hi,
I modified line number 5 and I got error in below:
Error using sixhrs (line 5) Some hour values are missing
So, could you kindly let me know where is the problem, please?
Hope to hear from you. Best Sepideh
SEPIDEH
SEPIDEH 2016 年 2 月 2 日
ok,
let me explain another time. I attached text file and the Matlab code which I modified here. If you run the code you will see the above error, which I mentioned.
Therefore, How is it possible to solve this problem? Hope to hear from you Thanks in advance for help Sepideh

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Import and Export についてさらに検索

質問済み:

2016 年 2 月 2 日

コメント済み:

2016 年 2 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by