How to use FIND function here?

I need to find the very first corresponding value of the payload when time is 0.0000.
Next,find the corresponding value of payload when time is 1.000.Likewise when time is 2.0000 and time is 3.0000. (pls note that payload column needs to convert to decimal values) [file is attached, herewith]
below is the code i used so far:
clc
clear all
fid=fopen('test.txt');
data=textscan(fid, '%*d%f%*s%*s%*s%*d%s', 40 ...
, 'MultipleDelimsAsOne',true, 'HeaderLines',1);
fclose(fid);
S=hex2dec(data{:,7});
Any suggestions?

2 Comments

"corresponding value of payload when time is 1.000." &nbsp closest to 1 or by integration or what does "corresponding" mean?
@ per isakson- oh...sorry for the confusion.
time payload converted value(used hex2dec fun for the payload col)
0.0000 03ff00000166 2.1518*1.0e+12
. ..... ........
. ..... .......
1.0993 03ff00000171 2.1518*1.0e+12
. ..... ....
. .... ....
2.0974 01f60000011a 2.1561*1.0e+12
Here, what i meant was: i need to get 1st and 3rd column as my output. Any idea how to solve it?

Sign in to comment.

 Accepted Answer

per isakson
per isakson on 13 Feb 2015
Edited: per isakson on 25 Feb 2015
"I need to get 1st and 3rd column as my output." &nbsp Is this what you want?
fid=fopen('test.txt');
data=textscan(fid, '%*d%f%*s%*s%*s%*d%s', 40 ...
, 'MultipleDelimsAsOne',true, 'HeaderLines',1);
fclose(fid);
S = hex2dec(data{2});
my_output = cat( 2, data{1}, S );
>> whos my_output
Name Size Bytes Class Attributes
my_output 40x2 640 double
"How to use FIND function here?" &nbsp I cannot see how find could be used here.
&nbsp
Addendum with inputs from the comments
Add the following lines to the script above.
sbs = floor(my_output(:,1)) + 1;
val = accumarray( sbs, my_output(:,2), [], @mean );
t1 = accumarray( sbs, my_output(:,1), [], @min );
t2 = accumarray( sbs, my_output(:,1), [], @max );
out = [ t1, t2, val ];
fprintf( '%g, %g, %g\n', transpose( out ) )
The script will now output
0, 0.998058, 4.39375e+12
1.09832, 1.99722, 4.39375e+12
2.09746, 2.99636, 4.39375e+12
3.09557, 3.89427, 4.39375e+12
&nbsp
A second addendum with inputs from the comments
Replace
val = accumarray( sbs, my_output(:,2), [], @mean );
by
first_value = @(vec) vec(1);
val = accumarray( sbs, my_output(:,2), [], first_value );
to get "very first corresponding value of the payload".
From testing and the documentation I understand that accumarray does not keep the order of the input values.
Correction: "does not keep" should be "does keep". If not, the function, first_value would be useless.

9 Comments

Thank you for your response. Appreciate your effort. But time column goes like this as follows:
time
0
0.1002
0.1995
0.2996
0.3999
0.4990
0.5993
0.6984
0.7987
0.8989
0.9981
1.0983
1.1985
1.2977
1.3980
What i want is : 0.0000 - 0.9981 take the value as 4.3938*1.0e+12.
Likewise, take 1.0983 to 2.0000(in the time column) take it as 4.3938*1.0e+12(again, this value is taken from the variable S).Eventually my goal is to plot a discrete time signal using these 2 columns. So for that i need to group the time column as 0.0000-0.9999, 1.0000-1.9999, etc. (see below)
time output
0.0000 - 0.9981 4.3938*1.0e+12
1.0983 - 2.0000 4.3938*1.0e+12
@ per isakson, i am really sorry to confuse you. When i run your code, the output for the 3rd column works well but the 1st column gives all the time values as zero, which is untrue. According to your output i am getting the following output:
0 4.3938*1.0e+12
0.0000 4.3938
0.0000 4.3938
0.0000 4.3938
0.0000 4.3938
0.0000 4.3938
0.0000 4.3938
0.0000 4.3938
0.0000 4.3938
But my whole point is for each category(time column) from 0.0000-0.9999 find the 3rd column value. Next category:1.0000 - 1.9999 find the 3rd column value.etc...
The first columns contains the values of the second column of the text file, i.e. time.
>> my_output(:,1)'
ans =
Columns 1 through 9
0 0.1002 0.1995 0.2996 0.3999 0.4990 etc.
"my whole point is for each category(time column) from 0.0000-0.9999 find the 3rd column value" &nbsp I didn't understand that from your original question. It is not obvious.
I've added code to accomplish "whole point" to my answer. Possibly, this could have been achieve easier with Matlab's new feature, Tables, Arrays in tabular form whose named columns can have different types
@ per isakson- thank you so much for your response. Really appreciate your effort. Can you be kind enough to explain what are these 2 lines denote?
sbs = floor(my_output(:,1)) + 1;
val = accumarray( sbs, my_output(:,2), [], @mean );
per isakson
per isakson on 14 Feb 2015
Edited: per isakson on 14 Feb 2015
See the documentation: accumarray, Construct array with accumulation. That's better than my words and there are plenty of examples.
@ Per isakson- Thank you so much !!! It works well. Really appreciate your effort.
per isakson
per isakson on 21 Feb 2015
Edited: per isakson on 21 Feb 2015
Comment from Chathu: [...] what function do I have to use in order to get the "real values" but not the "mean"?
In the original question you say "find the very first corresponding value". I had forgotten that when I wrote @mean. Does "real values" refer to this "first [...] value"?
@ per isakson- Just now saw this message. Thanks ALOT for your response. It is very kind of you to reply me even after accepting your answer. Your genuine support is truly admired. Yes, with your help i managed to solve my qu. Keep up the good work. Thanks!

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 13 Feb 2015

Edited:

on 25 Feb 2015

Community Treasure Hunt

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

Start Hunting!