subtracting and saving in an array

1 view (last 30 days)
Chathu
Chathu on 16 Jan 2015
Edited: Stephen23 on 25 Jan 2015
My data looks like this, where column headers are No and Time,respectively.
'1' '0.000000000'
'2' '0.100244000'
'3' '0.199460000'
'4' '0.299659000'
'5' '0.399856000'
'6' '0.499070000'
.
.
'50' '4.893317000'
'51' '4.993553000'
'53' '5.095700000'
'55' '5.194844000'
'57' '5.295069000'
How to find:
  1. I want to find the values for (0.000000000 - 0.100244000), (0.100244000 - 0.199460000) and write the answers in a separate array.(eg:No, Time, and Time difference).
  2. In the 1st column,after value 51, next value is 53. I want to count how many values have been jumped like that instead of following a sequence as 51,52,53...
  3 Comments
Chathu
Chathu on 19 Jan 2015
Image Analyst- these data are stored in a notepad. using text scan i have imported it to MatLab. But i want to find the time difference(which is the 2nd column) for each No.(which is the 1st column). Any suggestions?
Chathu
Chathu on 20 Jan 2015
Does anyone have any idea how to find the time difference(in the 2nd column)?
And how to see how many values have been jumped(in the 1st column)?
Really appreciate your feedback.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 20 Jan 2015
Edited: Stephen23 on 20 Jan 2015
Based on the text file that you uploaded, I altered my first answer to read all of the columns in your data:
fid = fopen('temp.txt', 'r');
data = textscan(fid, '%d%f%s%s%s%d%s', 'MultipleDelimsAsOne',true, 'HeaderLines',1);
fclose(fid);
jumps = sum(abs(diff(data{1})>1));
timeD = diff(data{2});
After placing this in a script and running it, I can confirm that it correctly extracts the data from your sample data file:
>> jumps
jumps =
1
>> data{1}
ans =
1
2
3
4
5
7
8
9
10
11
12
13
14
15
Note that I altered your text file by removing data row six to provide a jump in the first column, which it seems you want to detect, otherwise it is identical to what you uploaded earlier.
  10 Comments
Stephen23
Stephen23 on 22 Jan 2015
Of course. It concatenates together two vectors: one of timeD values, and one of row No. values, which are then displayed using the fprintf command. Lets have a look at its constituent parts.
Firstly have a look at the line idx = diff(data{1})==1;, this defines a logical vector which is true only where there is no jump, and false where there is a jump. You can look at its values in the variable viewer panel (double click on any variable in your workspace to open it in the variable viewer).
Then with timeD(idx) we use logical indexing to obtain only those timeD values where there is no jump (e.g. excluding No. 7 in my example data, as No. 6 is already absent). These data are of class double.
The code data{1} gets us our first column of data, the No. values, which are of integer data class. Using data{1}([false;idx]) we again select only the ones that correspond to where there is no jump. I shifted the index by one place using the false value to try to make it slightly more intuitive to read when it is displayed. Hint: try removing this and see what happens.
We want to join these two vectors together (for displaying) but they are of different data classes, so we convert the No. values to double using double(data{1}([false;idx])).
Now we can concatenate them together using square brackets simply [double(data{1}([false;idx])),timeD(idx)]. Because MATLAB is row-major the final step is to transpose this matrix with .', so that each column contains one No. value and one timeD value. This gives us the final code segment [double(data{1}([false;idx])),timeD(idx)].', which is then displayed ( without loops !) using fprintf.
Here is a neat trick in MATLAB that you should try: highlight any piece of code and press the key f9 (on the very top of your keyboard) to evaluate that highlighted section of code. You can use this trick to look at the individual parts of any line of code and see what they evaluate to. Try it by highlighting timeD(idx) in your script, press the f9 button, and see how it is different to plain-old timeD !
Stephen23
Stephen23 on 22 Jan 2015
Edited: Stephen23 on 25 Jan 2015
EDIT: The OP deleted their previous comment above.
This is really a new topic, so it deserves a new question.
however...
You code is almost right, just the function floating is not known to MATLAB. It doesn't exist, unless you have written it yourself, and gives you an error ("Undefined function..."). But that is alright, because the values that we have in data{2} are already floating point (that array is of class double), so we don't need to convert the second column values to another data type. Try this:
num = data{2} < 121;
sum(~num)

Sign in to comment.

More Answers (3)

Ilham Hardy
Ilham Hardy on 16 Jan 2015
A wild guess, you may need:
  1. cell2mat (to convert your cell into workable matrix)
  2. diff (to find the difference between adjacent elements)
  3. find (to find how many values that you have been jumped)
You can search the documentation by typing eg. doc cell2mat into the command window.
  4 Comments
Ilham Hardy
Ilham Hardy on 20 Jan 2015
Seeing several answers below and you have not solve your problem, i would say that it might be better if you can upload your .txt file so that we could see what are the actual problems.
If it too big, you can copy several (10-20) lines of your .txt file. Use the attach (paperclip) button for attaching file.
Chathu
Chathu on 20 Jan 2015
llham- you are right. I would have upload the attachment earlier-my mistake..:( Thanks alot for all who have shed me some light to solve this issue. Especially Stephen,thank you soo much for your continuous support. it is simply wonderful. Thanks alot.

Sign in to comment.


Stephen23
Stephen23 on 20 Jan 2015
Edited: Stephen23 on 20 Jan 2015
I saved the data that you gave in a comment to Ilham Hardy's answer in a simple text file "temp.txt", removing row six to get a jump in the first column:
'1' '0.000000000' '01f600000234'
'2' '0.100244000' '01f700000235'
'3' '0.199460000' '01f600000236'
'4' '0.299659000' '01f600000237'
'5' '0.399856000' '01f600000238'
'7' '0.599265000' '01f60000023a'
'8' '0.699509000' '01f60000023b'
Then the following code can be used to extract this data and convert it into numeric & string data:
fid = fopen('temp.txt', 'r');
data = textscan(fid, '%d%f%s', 'MultipleDelimsAsOne',true, 'Whitespace','''\b\t');
fclose(fid);
You can then find how many jumps there are in the first column using this code:
jumps = sum(abs(diff(data{1})>1));
And the time differences from the second column
timeD = diff(data{2});
  7 Comments
Stephen23
Stephen23 on 20 Jan 2015
Edited: Stephen23 on 20 Jan 2015
Please try this exactly:
  • Copy the data from my answer into a text file, and save this as "temp.txt" in your current directory. Do not use your data.
  • Copy all of the code from my answer into an Mfile (script), and save this as "temp.m" in your current directory.
  • Run the script temp.m.
Do not make any changes to the code or the data. Does this really result in empty arrays?
In your question you wrote that the data has two columns. Then you said later that it has three. Now it turns out to have seven. While I might be able to cope with these random changes to your data, MATLAB cannot. Did you read my earlier comment: "These differences are however very significant when importing into MATLAB, and you must describe that data file format exactly as it is." Please help us by being clear and giving the details that we request. For example I asked you six specific questions about the formatting of your data, and you answered one, even though all of these answers could have a significant effect on how the data needs to be imported.
MATLAB is very precise with importing data, because it has so many options and can deal with many different formats, but you need to be able to define the file format yourself. Some tools semi-automate this:
Stephen23
Stephen23 on 20 Jan 2015
Edited: Stephen23 on 20 Jan 2015
Exactly how huge is this "huge" file? Before sending me the whole data, you can upload a subsample of the data file here. Take the first 100 lines or so, and save these in a new file. Please upload this file in a new comment (use the paperclip button above the text field). I will have a look at it and see how we can get this thing working for you :)

Sign in to comment.


Chathu
Chathu on 20 Jan 2015
Edited: Chathu on 20 Jan 2015
PS: i am adding this to here because the question is totally related to the same question.
Suppose i want to write a for loop for this question.
Eg: suppose that i want : (abs(diff(data{1})==1)) & diff(data{2})
so my for loop would be:
n=15;
for i=1:n
for j=1:2
if (abs(diff(data{1})==1))& find timeD=diff(data{2})
fprintf('timeD value is:%6.5d\n',i)
else disp('timeD is:skipped')
end
end
end
Any suggestions?

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!