# subtracting and saving in an array

5 visualizzazioni (ultimi 30 giorni)

Mostra commenti meno recenti

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:

- 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).
- 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 Commenti

### Risposta accettata

Stephen23
il 20 Gen 2015

Modificato: Stephen23
il 20 Gen 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 Commenti

Stephen23
il 22 Gen 2015

Modificato: Stephen23
il 25 Gen 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)

### Più risposte (3)

Ilham Hardy
il 16 Gen 2015

A wild guess, you may need:

- cell2mat (to convert your cell into workable matrix)
- diff (to find the difference between adjacent elements)
- 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 Commenti

Stephen23
il 20 Gen 2015

Modificato: Stephen23
il 20 Gen 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 Commenti

Stephen23
il 20 Gen 2015

Modificato: Stephen23
il 20 Gen 2015

### Vedere anche

### Categorie

### Community Treasure Hunt

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

Start Hunting!