Octave: Subscripts for the text function? - text

I'm running Octave 5.1.0. I try to reproduce the code on this page:
https://octave.sourceforge.io/octave/function/text.html
For example, I try to reproduce Demonstration 3 there, i.e. this code:
clf;
axis ([0 8 0 8]);
title (["1st title";"2nd title"]);
xlabel (["1st xlabel";"2nd xlabel"]);
ylabel (["1st ylabel";"2nd ylabel"]);
text (4, 4, {"Hello", "World"}, ...
"horizontalalignment", "center", ...
"verticalalignment", "middle");
grid on;
I get the following error message:
text(,,,,,,): subscripts must be either integers 1 to (2^63)-1 or logicals (note: variable 'text' shadows function)
I tried changing the code to:
clf;
axis ([0 8 0 8]);
title (['1st title';'2nd title']);
xlabel (['1st xlabel';'2nd xlabel']);
ylabel (['1st ylabel';'2nd ylabel']);
text (4, 4, 'Hello','horizontalalignment', 'center','verticalalignment', 'middle');
grid on;
Then I get the following error message:
text(4...[x6]...): but text has size 1x39 (note: variable 'text' shadows function)
I'm a bit stumped as to how to solve this. I also fail to reproduce other code with the text function. I started running Octave a few weeks ago, so it wouldn't be impossible that my installation failed somehow. Other functions work as expected though.
I have installed the io and statistics packages. Can they interfer somehow?
Can anybody figure out what's going on?

The key is in the error message:
note: variable 'text' shadows function
It means you have defined "text" as a variable in your workspace, and have assigned a value to it.
Therefore, now each time you try to access the text function, you are accessing your variable instead.
Clear your workspace (or just the text variable) and try again.
PS. While this is not conventional practice, I personally tend to name all my non-function variables starting with a capital letter, to avoid name-clashes with functions, since most functions in octave start with a small letter.
In general, always check that a name is not taken before you assign something to that name, to avoid 'shadowing' as in this case.

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

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

Use stats inside Gnuplot inline functions

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)

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'.

How to define a function without arguments in gnuplot?

I want to define a function which returns a random number:
gnuplot> rnd() = int(rand(0)*2**24)
^
invalid command
I need add a parameter x to stop the error message:
gnuplot> rnd(x) = int(rand(0)*2**24)
Is it possible to define a function without parameters?
I don't think that is possible (look at the syntax for functions in help user-defined). As far as I can tell -- using rand is the only time when this is useful -- In all other cases your "function" would just evaluate to a constant. I'm guessing the gnuplot devs just haven't thought of this (interesting) corner case, or they didn't think it useful enough to implement.
I hope that I'm wrong about this and somebody else comes along and proves it, but I don't think I am. I think the easiest workaround it to just pass a parameter as you've already mentioned. If you really don't like that, you could use a macro:
set macro
rnd="(int(rand(0)*2**24))"
print 5+#rnd

Resources