subtracting and saving in an array
Ältere Kommentare anzeigen
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 Kommentare
Image Analyst
am 16 Jan. 2015
Is your data in a cell array or a table? Or something else like a structure array?
Chathu
am 19 Jan. 2015
Chathu
am 20 Jan. 2015
Akzeptierte Antwort
Weitere Antworten (3)
Ilham Hardy
am 16 Jan. 2015
0 Stimmen
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 Kommentare
Chathu
am 16 Jan. 2015
Chathu
am 16 Jan. 2015
Ilham Hardy
am 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
am 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 Kommentare
Chathu
am 20 Jan. 2015
Try this exactly:
- Copy the data from my answer into a text file, and save this as "temp.txt".
- Copy the code from my answer into an Mfile, and save this as "temp.m".
- Run the script temp.m.
This does not result in empty arrays. The fact that it does not work on your data means that your data is different somehow to what you are describing to us. It may be that your data is either incomplete (has some missing or values of a different type) or has different delimiters, or some other subtle difference. These differences are however very significant when importing into MATLAB, and you must describe that data file format exactly as it is.
- Does the data file really include those single quotation marks around every field (including numeric)?
- Are there really multiple delimiters between every field?
- Are there really leading delimiters (as your original question has)?
- How many columns does the data file contain?
- Does the data file have a header, a footer, any text lines or any blank lines?
- You wrote that "these data are stored in a notepad", however "Notepad" is not a file format, it is a Windows program for editing text files. Is the data stored in a text file?
The best solution would be for you to upload the data file, and I can adapt the code to work with your exact data.
Chathu
am 20 Jan. 2015
Stephen23
am 20 Jan. 2015
Sure, send me a massage via my profile page.
Did my example work when you tried it exactly as I described?
Chathu
am 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:
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 :)
Kategorien
Mehr zu Mathematics finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!