I have a data set on disk that I'm reading line by line, and I would like to convert one of the columns of data into a vector of floats (with a range of 0-23.99999) (for that day).
The data looks something like the following:
2010/01/01,00:00:00.979131, 27.4485, 51.9362, 14.8, 6
2010/01/01,00:00:01.021977, 27.5149, 51.9375, 16.0, 6
2010/01/01,00:00:01.074032, 27.4797, 51.9446, 14.5, 10
2010/01/01,00:00:01.663689, 25.8441,-152.8141, 14.6, 6
2010/01/01,00:00:01.639541, 25.8744,-152.6122, 1.5, 5
2010/01/01,00:00:02.232099, -2.2447, 11.5023, 18.8, 6
2010/01/01,00:00:02.256351, -0.8135, 27.3139, 17.7, 5
2010/01/01,00:00:02.306734, -2.7797, 28.5109, 26.0, 5
2010/01/01,00:00:02.620765, 25.6656,-154.2029, 26.2, 9
2010/01/01,00:00:02.658495, 25.6698,-154.2157, 23.0, 6
2010/01/01,00:00:02.731266, -5.7106, 126.4517, 3.6, 5
2010/01/01,00:00:02.787495, -5.7138, 126.5210, 24.4, 8
2010/01/01,00:00:02.811636, -3.2453, 124.6919, 21.1, 8
column 2 (e.g., 00:00:00.979131) is of interest and I would like to do something like
setenv GNUTERM 'x11';
fid = fopen('myfile.txt', 'r');
m = textscan(fid, '%d%d%d%d/%d%d/%d%d, %d%d:%d%d:%d%f, %f, %f, %f, %d');
mx = m(:, 5); %here, I would expect to grab 14.8, 16.0, etc
my = m(:, 2) / 24.0; %here, all data from timestamp column (00:00:00.979131, for ex)
plot(mx, my);
The issue is that the string I pass to textscan is malformatted for my data.
The formatting of that number is "hrs:minutes:seconds", in military time.
How can I access/convert the values for the vars mx, or my?
Thanks,
jml
The output of textscan is a cell array. If you use the command in your answer:
m = textscan(fid, '%d/%d/%d %d:%d:%f %f %f %f %d', 'delimiter', ',');
Then to get a vertical vector of 14.9, 16.0, 14.5:
MyNinthField = m{9};
MyNinthField =
14.8000
16.0000
14.5000
14.6000
1.5000
18.8000
17.7000
26.0000
26.2000
23.0000
3.6000
24.4000
21.1000
Then, to get the timestamp (seconds since the beginning of the day):
Hours = double(m{4});
Minutes = double(m{5});
Seconds = m{6};
For Seconds double is not needed, because m{6} is already double. However, m{4} and m{5} are both int32.
To get the time of the day in seconds, all you need is:
TimeOfDayInSeconds = 3600*Hours+60*Minutes+Seconds;
TimeOfDayInSeconds =
0.97913
1.02198
1.07403
1.66369
1.63954
2.23210
2.25635
2.30673
2.62077
2.65849
2.73127
2.78749
2.81164
If you didn't do type conversion from int32 to double, Octave would truncate the values to integers. MATLAB however does not even allow the sum between integer and double arrays.
Hope this helps.
Looks like you can do something like:
m = textscan(fid, '%d/%d/%d %d:%d:%f %f %f %f %d', 'delimiter', ',');
...which, although it works, it puts the time format var that I wanted into multiple portions of a var. Better than nothing. If anyone has suggestions on how to concatenate those variables after, I would love to read more. Thanks!
Related
In a table having an integer primary key of indexRow in which the last two digits are currently 55, I'd like to change that to 50 but only if the column added is an integer value 55 and the indexRow ends in 55. I'm using SQLite.
I tested it as follows. Would you please tell me if this is the correct approach (if there is a better method) because I'd like to use it to run an update on the table?
Of course, I'll do it within a transaction and test before committing; but wanted to ask. I expected to have to use some math to determine which indexRows ended in 55, but converting to string seems quite easy.
select indexRow, indexRow-5, substring(format('%s', indexRow),-2)
from newSL
where added=55
and substring(format('%s', indexRow),-2)='55'
limit 10;
indexRow indexRow-5 substring(format('%s', indexRow),-2)
----------- ----------- ------------------------------------
10080171455 10080171450 55
10130031255 10130031250 55
10140021655 10140021650 55
10140080955 10140080950 55
10240330155 10240330150 55
10250230555 10250230550 55
10270031155 10270031150 55
10270290355 10270290350 55
10300110355 10300110350 55
10300110455 10300110450 55
Yes, use the modulo operator, %. In the expression x % y, the result is the remainder of dividing x by y. Therefore, 4173 % 100 = 73.
Note that % is a math operator, just like * for multiplication and / for division, and is not related to using the % in the format function.
I am plotting a set of data that is from 5E-5 to 4E-4.
In order to make the plot according to a editor's request I wrote in the title of the axis (x10^-5) as units.
So, in the Y axis I want to see the tics: ... 8, 9, 10, 11, 12 ...
I have played with the specifiers for example
set format y "%1.1t"
instead I get in the tics 8.0, 9.0, 1.0, 1.1, 1.2
How can I get the values ... 8, 9, 10, 11, 12 ... as tics?
Thanks.
The best format ever with gnuplot is :
set tics format "%.1s%c"
;=> change 10e3 into 10k, 10e6 into 10M, kilo, Mega, Giga, Peta etcétéra
;=> show 10e-3 into 10m, 10e-6 into 10u mili, micro, nano femto etcétéra
For your purpose, you want no suplementary digit after the dots, so your answer is :
set tics format "%.0s%c"
I'm trying to retrieve data from a DPO3034 scope by sending these these commands:
DATA:SOURCE CH1
DATA:ENCDG ASC
DATA:START
CURVE?
I get 98, 98, 98, 97, 97, 98, 98,...
How can I convert these ASCII formatted values to voltages?
I also tried retrieving data that are binary formatted
DATA:SOURCE CH1
DATA:ENCDG RIBINARY
DATA:START
CURVE?
I get #520000a b a b b a b c b c a b a a a b ^ b b a b a b....
How can I convert these to proper data points?
Command reference for the DPO3000
These values you are reading using CURVE? are digital value that has gain and offset for turning them into (usually) volts.
You should read also these values:
double YZero = double.Parse(io.Query("WFMO:YZE?"));
double YMult = double.Parse(io.Query("WFMO:YMU?"));
double YOff = double.Parse(io.Query("WFMO:YOF?"));
And then you should calculate the real voltage from each value rawValue in the array you get from CURVE?:
double voltValue = YZero - YOff * YMult + (YMult * double.Parse(rawValue));
same goes for binary data, just parse it into int16 (depending on the bit-length of each number)
P.S.
I believe your manual is not the latest, I'm recommending downloading these from Tektonix website.
I am using SpreadsheetGear and I want to set format for a column as %. currently I am using following code:
worksheet.Cells[0, index].EntireColumn.NumberFormat = #"##0.00\%;[Red](##0.00\%)";
The above code apply formatting which converts:
6 = 6.00%
6.02 = 6.02%
.02 = .02%
But the problem is when I use this value and perform a math operation, excel consider actual value. Example: if a cell is showing 20.00% and I multiply it with 10 it gives me 200.00.
What I want is: When any numeric operation is performed & column type is defined as % system should divide the value by 100 and then it should perform the opeartion.
so if I multiple 24.00% with 10 it should give me .24.
Thank you
If you escape the % (\%), Excel just adds the % and does not adjust the decimal. For example, the following code
//with escape
cells[0, 0].NumberFormat = #"##0.00\%;[Red](##0.00\%)";
cells[0, 0].Value = 0.24;
//with no escape
cells[1, 0].NumberFormat = #"##0.00%;[Red](##0.00)%";
cells[1, 0].Value = 0.24;
//with no formatting
cells[2, 0].Value = 0.24;
cells[0, 1].FormulaR1C1 = "=RC[-1]*100";
cells[1, 1].FormulaR1C1 = "=RC[-1]*100";
cells[2, 1].FormulaR1C1 = "=RC[-1]*100";
will produce
0.24% 24 //with escape
24.00% 24 //with no escape
0.24 24 //with no formatting
Your problem is a math problem.
24% x 10 = 240%
240% = 2.4
"so if I multiple 24.00% with 10 it should give me .24"
If all you are trying to do is show a different output, then the system should interpret multiplying by 10 as removing the % formatting. This is because 24% and .24 are the same numbers. You are basically asking the machine to do multiplication incorrectly. Which is fine if that is what your client really really wants.
I'm writing a task in pascal.
Everything is ok, just my result is not right.
I'm summing some numbers
Example: 2.3 + 3.4+ 3.3 = 9
But output shows: 9.000000 + EEE or something like that.
So- how to convert, to be only 9, not this REAL variable.
To actually convert:
var
i: integer;
...
i := round(floatVar);
To output only the integer part:
writeln(floatVar:9:0);
Let's consider this quite simpler equation:
3.5 + 2.5
What do you expect? 6, right? Let's try this code
write(3.5 + 2.5);
Unfortunately, it's a floating-point number, so it would produce a number represented in a scientific way:
6.00000000000E+00
or 6.0000000000 x 100, or 6 x 10o. Whatever, you only care about 6, who need this weird useless long number? So the idea is to cut off the decimal part and output to the console only the integer part, which can be done with this line of code:
write(3.5 + 2.5 : 0 : 0);
Ok, now it outputs a beautiful number as expected
6
Seems like the problem is solved, but you say that:
I'm summing some numbers
Example: 2.3 + 3.4+ 3.3 = 9
Ohh so that the evenly, beautiful integer is just randomly appeared? Here the problem comes, how do you expect this equation would output?
3.6 + 2.5
It should be 6.1, right? Let's try it with the worked line of code:
write(3.6 + 2.5 : 0 : 0);
And the output is...
6
Unexpected, right? So how about rounding to some decimal places, like 1?
write(3.5 + 2.5 : 0 : 1);
write(3.6 + 2.5 : 0 : 1);
Then, 3.5 + 2.5 = 6.0 and 3.6 + 2.5 = 6.1. But 6.0 may look quite long, so how to make it output 6 for 6.0 and 6.1 for 6.1?
Actually, you can't make the program auto-detect if a real variable contains an integer value because the way a real var is stored is completely different from an integer var (how different they are, please contact Google; but you can do it manually by making a function to do the job).
So my solution is, to be easy, making the output rounded to some decimal places, and that's it.
For purpose of showing pretty output on the screen you can use something like this:
Writeln(result:0:2);
Result on screen would be this:
9.00
What this means someone would ask? Well first number 0 means how wide filed is. if you say it's 0 then Pascal writes it at the very left side of screen. If you said writeln(result:5:2) result would be:
9.00
In other words i would print form the right side and leave 5 chars to do so.
Second number 2, in this example means you want that result printed with 2 decimal places. You can place it only if you want to print on screen value that is real, single, double, extended and so on.You can round to any number of decimals, and if you do writeln(result:0:0) you would get ouput:
9
If you are printing integer and want to have some length of field, lets sat 5 you would do: writeln(int:5). If you added :2 to the end you would get compile time error.
This all also works for something like this: writeln(5/3.5+sqrt(3):0:3),
You should know that this does not round variable itself but just formats output. This is also legal:
program test;
var
a:real;
n,m:integer;
begin
readln(a,m,n);
writeln(a:m:n);
end.
What i did here is i asked user if on how many decimals and with what length of field he wants to write entered number a. This can be useful so i'm pointing it out. Thank you for reading. I hope i helped
You can convert to string, get the int part, e convert to int number!
Or Float to Str than Str to Int:
nPage := StrToInt(FloatToStr(Int(nReg / nTPages))) + 1;