How to convert string to number in gnuplot
You can add 0 to the string:
stringnumber="1.0e0"
number=stringnumber + 0
plot [-1:1] number
Convert string to integer in gnuplot
The variable i
in [i in columns]
is actually a string. So, convert it to an integer via int(i)
.
If you want to plot a column defined by an integer variable, use ... using (column(i))...
.
So, in combination:
Code:
### plot column numbers from a sequence in a string
reset session
set key top left
$Data <<EOD
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45
51 52 53 54 55
EOD
columns="2 3 5 1 4"
plot for [i in columns] $Data using 1:(column(int(i))) w lp title sprintf("Column %s",i)
### end of code
In your case remove the datablock $Data <<EOD ... EOD
and in the plot command replace $Data
with "mydata.dat"
Result:
Conversion string column into numeric column to plot in GNUPLOT
Is this what you need?
set xdata time
set timefmt "%Y-%m-%d"
set format x "%Y-%m-%d"
f(x) = x eq "True" ? 1:0
plot 'data.txt' u 1:(f(strcol(2))) w lp
This will use f(x)
to convert True/False into 1/0 values. The rest is simply date formatting.
How do I convert an integer to a string in gnuplot?
Are you looking for something like:
plot for [c=1:5] datafile using (column(c)):(column(c+1))
This will do: plot datafile u 1:2, "" u 2:3, "" u 3:4, "" u 4:5, "" u 5:6
gnuplot - convert a string variable to lowercase
The full function Macro solution (Thanks theozh) has me thinking again of how to implement this as a function. The idea of using a lookup table to convert characters by equating an ordinal number was a great idea. Encapsulating single character case conversion into a function was the start, and then along with recursion, it has made it possible to handle full strings as I had first looked for. I hope this is now a tidy solution for all. Share and enjoy.
# GNUPLOT string case conversion
# string_case.gnu M J Pot, 14/1/2019
# Index lookup table strings
UCases="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
LCases="abcdefghijklmnopqrstuvwxyz"
# Convert a single character
# Char to convert is added to string so it is always found to default other chars
toupperchr(c)=substr( UCases.c, strstrt(LCases.c, c), strstrt(LCases.c, c) )
tolowerchr(c)=substr( LCases.c, strstrt(UCases.c, c), strstrt(UCases.c, c) )
# Convert whole strings
# Conversion first char (or return null), and recurse for the remaining
toupper(s) = s eq "" ? "" : toupperchr(s[1:1]).toupper(s[2:*])
tolower(s) = s eq "" ? "" : tolowerchr(s[1:1]).tolower(s[2:*])
Addition: Improved solution
This is a re-work of the recursive case conversion as self contained functions. A little more effort has resolved the excessive stack usage of the first solution. I had only been considering strings of single words when I had the problem. Note:- the single character conversion has been made more robust.
# GNUPLOT string case conversion
# string_case.gnu M J Pot, 29/1/2019
# toupper(), tolower() functions
# Index lookup table strings
UCases="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
LCases="abcdefghijklmnopqrstuvwxyz"
# Convert a single character
# Char to convert is added to string so it is always found to default other chars
# Null strings are returned null
toupperchr(c)= c eq "" ? "" : substr( UCases.c, strstrt(LCases.c, c), strstrt(LCases.c, c) )
tolowerchr(c)= c eq "" ? "" : substr( LCases.c, strstrt(UCases.c, c), strstrt(UCases.c, c) )
# Divide & conquer
# A simple linear recursive call uses too much stack for longer strings.
# This undertakes a binary tree division to make the stack growth order log_2(length)
toupper(s) = strlen(s) <= 1 ? toupperchr(s) : toupper( substr(s,1,strlen(s)/2) ) . toupper( substr(s,(strlen(s)/2)+1,strlen(s)) )
tolower(s) = strlen(s) <= 1 ? tolowerchr(s) : tolower( substr(s,1,strlen(s)/2) ) . tolower( substr(s,(strlen(s)/2)+1,strlen(s)) )
splitting a number into the integer and decimal parts GNUPLOT
If you want to include a float number into a title or a file name use the string formatting via sprintf()
, check help sprintf
and help format specifiers
.
a = 1.234
myFile = sprintf("MyFileName_%g_MoreParameters.png",a)
print myFile
Result:
MyFileName_1.234_MoreParameters.png
Gnuplot: Convert integer to ASCII value
gnuplot> print sprintf("%c", 65)
#A
Gnuplot provides a gprintf which uses gnuplot format specifiers and sprintf.
gnuplot - how to convert read time format to double/float/use it as function parameter
Internally, gnuplot stores the time as the number of seconds since some epoch (the epoch is different between versions, but is the Unix epoch in the 5.0 line).
Normally, using the time format settings, gnuplot handles conversions itself. However, there are functions that can be used for processing the times manually as well, for example
strptime("%M:%S","50.254")
will process the time of your first line into a number of seconds. We can even use this in plotting commands, as long as we are careful to read columns as strings
plot "data.dat" u 0:(strptime("%M:%S",strcol(1)))
will plot your data file as seconds elapsed versus the line number. As you are not specifying date info in your time format, gnuplot defaults to the beginning of it's first day in the epoch, so we just get 50 some seconds (otherwise the numbers could be much larger).
In general, you can handle any time format like this. It may be a little difficult if you have spaces or such in your time format, as you won't be able to rely on gnuplot's ability to know how much to "chunk" into the column without setting a time format (but this can be handled by using a different separator or quoting times).
See help strptime
for the function which converts the time format to the internal representation and help strftime
for the function which converts one of these internal representations back to a date string.
Related Topics
Git Pull: Change Authentication
Enable/Disable Tasks in Crontab by Bash/Shell
How to Get the First Column of Comm Output
How to List All Users in a Linux Group
How to Create Threads Without System Calls in Linux X86 Gas Assembly
Delete the First Five Characters on Any Line of a Text File in Linux with Sed
Call a Kernel Module Function from Program at User Space
How to Delete the First Column ( Which Is in Fact Row Names) from a Data File in Linux
Can You Enter X64 32-Bit "Long Compatibility Sub-Mode" Outside of Kernel Mode
Node Server Crashes After Few Hours
Possible to Use a .Dll on Linux
How to Check Status of Urls from Text File Using Bash Shell Script
Google-Chrome Failed to Move to New Namespace
Fastest Way to Tell If Two Files Have the Same Contents in Unix/Linux
What Is Raw Socket in Socket Programming
Installing R from Cran Ubuntu Repository: No Public Key Error
Performance Difference Between Ipc Shared Memory and Threads Memory