Use stats inside Gnuplot inline functions - gnuplot

I can't figure out if it is possible to do something like
f_L0(FL)=(stats FL using ($8) name "L", \
L = L_mean, \
1)
Gnuplot keeps complaining: ')' expected and points to FL inside the function.
Any ideas how to define some function like this?
p.s. my gnuplot is 4.6 (Feb2014).
UPDATE:
since this is not possible, it seems, i would just do some analysis in Octave and output it to the file :)

No, you cannot use stats inside of inline funcitons. Gnuplot functions are very basic and can contain only 'regular' mathematical function which compute a single numerical value. You cannot get access to data files inside of functions.
The stats command is command, which is the only command besides plot and splot which has access to data files.
The use of stats is as follows: You must use it as standalone command like
stats 'filename' using 8 name "L"
after which you have access to many variables which contain results from calculations on your data (column 8 in file filename). L_mean is one of those variables, for more see show variables after having executed stats.
However, you can do many calculations inside the using statement. To subtract the mean from you data, use e.g.:
stats 'filename' using 8 name "L"
plot 'filename' using ($8-L_mean)

Related

How to convert integer into string gnuplot

My code is the following, I would like a to assume iteratives values like '3' , '4' and so on. My code is like:
a=2
#perform some basic operation like:
b=a*2
#convert it to string
c=str(b)
p path1 u 1:($1<=0?$#c:1/0) w filledcurves y=0
The solution proposed on similar topic here so far did not work.
You should use this syntax
c=''.b
c will be equal to 'b'
gnuplot provides both sprintf (as in the C language routine) and a private implementation gprintf that offers formatting options beyond the normal ones provided by the C language. The full details with all supported format options are in the gnuplot documentation. A very simple use would be:
c = sprintf("%8.3f", b)
However, it makes no sense to convert the value to a string if your intent is to use it in a plot command that expects a number. There is no iteration in the pseudo-code you show so I can't guess exactly where you are headed with this but note that the operation #c to evaluate c as a macro expansion inside an iteration will always yield the content of c as it was prior to the iteration. So using the # operator inside an iteration is almost always wrong.
If I correctly understand your minimal incomplete non-working example, I can only guess what you probably wanted to do, i.e. limit the plot of a dataset in a certain column which is selected by some variable. Correct? If my guess is true, I would do it the following way, no need for string conversion and the use of macros. Check help column.
a=2
# perform some basic operation like:
b=a*2
plot path1 u 1:($1<=0 ? column(b) : NaN) w filledcurves y=0

How to retrieve non-zeroes using gurobipy?

I am using gurobipy to read LP files. The command
model=gurobipy.read("name.lp", env=env) gives me the number of rows, columns, and non-zeroes. However, I need to retrieve the number of non-zeroes. I don't believe there is a function that does this automatically (i.e. model.getnonzeros() )
Is there a way to obtain the non-zeros? How would I write python code to be able to do this if there isn't a built in function?
Consulted resources
Get constraints in matrix format from gurobipy
Ok I figured it out - perusing the Gurobi reference manual, in chapter 6, Python API overview, I see there is an attribute called "NumNZs" that can be called as:
print(model.getAttr("NumNZs"))
This will give the non-zeroes

Writing specific variable to .txt or .mat with Dymola

I am in need of a way to write a specific variable from a simulation to either .txt or .mat.
The use of:
Streams.print(Modelica.Math.Vector.toString(resultVector),"filename");
results in wrong data as in no acordance when compared to the export of the variable over the simulation tab. The values in the .txt file are completly different with no pattern visible.
Would Modelica.Utilities.Streams.writeRealMatrix do what you need?
For me
Modelica.Utilities.Streams.writeRealMatrix(
fileName="C:/temp/test.mat",
matrixName="testMatrix",
matrix=[1,2;4,5],
append=false,
format="7")
resulted in the expected .mat file.
For being able to write a variable in the .mat file, you will have to convert your variable to a (two-dimensional) matrix - if it isn't already one. This can be accomplished by converting a scalar or vector using square brackets. This would give matrix=[1] for a scalar or matrix=[{1,2,3,4}] for a vector. Using matrix=1 or matrix={1,2,3,4} will trow an error.
Since post-processing would be acceptable, there are some options available:
Dymola includes the tool alist:
alist -e var1 [-e var2 ...] inputFile outputFile
export data for var... as comma-separated values, suitable
for importing into Microsoft Excel.
Options: -a write in ASCII format (default)
-b write in Matlab binary format
https://www.j-raedler.de/projects/DyMat/ can be used to read the result-file and generate a file on any format you like (using Python).
OpenModelica has an option similar to alist that can also resample the signal if you want fewer data points https://openmodelica.org/doc/OpenModelicaUsersGuide/v1.12.0/scripting_api.html#filtersimulationresults.

GNUplot fit with only x-errors in version 5?

I'm trying to fit my data using GNUplot. As it happens my data only has x-error bars. I heard version 5 is supposed to allow fitting using x errors only. How can one do this? I tried the following but as you can see I get errors that I cannot figure out:
gnuplot> fit f(x) "data1m" using 2:3:4 with xerrorbars via b,u,n
warning:
> Implied independent variable y not found in fit function.
> Assuming version 4 syntax with zerror in column 3 but no zerror keyword.
^
Need via and either parameter list or file
How can I resolve this?
It doesn't seem to work with x errors only, but you could use a very small constant y error.
Try fit f(x) "data1m" using 2:3:4:(1E-38) xyerrors via b,u,n

Problems using matlab to print a histogram to a file

I am trying to create a histogram of numbers in an array. I am using Matlab to do this. I am connecting via ssh so I can only use Matlab in the terminal on my Linux computer. I am trying to create a histogram of the data in the array and save it as a .png. I know that in order for me to save this I need to use the print function. So far my attempt has been the following:
h=hist(array)
print(h,'-dpng','hist1.png')
which told me that there is no variable defined as -dpng but I thought that the point of that was to specify the file type.
Then I just deleted the -dpng and ran it as
print(h,'hist1.png')
to which it told me "Handle must be scalar, vector, or cell-array of vectors"
At this point I don't quite know what to do next. I would like for someone to help me figure out how to print this histogram to a .png file. Thank you.
hist does not return a figure handle, you could to do something similar to:
h = figure;
hist(array);
print(h, '-dpng', 'hist1.png');
to save the histogram.
By itself, the function hist(array) plots a histogram. If you assign the output to a variable, it returns the binned values of array, not the handle to your plot.
f = figure;
hist(array)
saveas(f,'hist.png')
you may would like to output the array to a csv file.
fid = fopen('file.csv','wt');
for i=1:size(arr)
fprintf(fid, '%s,%d,%d\n','element number' ,i ,arr(i));
end
fclose(fid);
See this link, you should be able to change the answers there to your needs: Outputing cell array to CSV file ( MATLAB )
You don't need to use figure handle unless you want to print not current figure. By default print uses gcf that returns handle for current figure.
So you just can do:
hist(array)
print('-dpng','hist1.png')
You got an error that there is no variable defined as -dpng probably because you forgot one quote symbol and used -dpng'.

Resources