How do I fix an error with dataaspectratio not being finite in Octave? - octave

I'm writing a simple script that iterates some data and plots the result. It was working fine. Then I zoomed in on the plot. After doing that, every time I try to run it I get the error
error: set: "dataaspectratio" must be finite
whether I use plot() or pcolor(). I found from a search that I can check the data aspect ratio with daspect() and the answer is [4 2 1] which looks finite to me. Even if I close and restart this error persists and won't let me plot anything, even a simple thing from the command line. Or a graph comes up with no y axis. How can I fix this?
The full error trying to run my file logistic.m is:
logistic
error: set: "dataaspectratio" must be finite
error: called from
__plt__>__plt2vv__ at line 495 column 10
__plt__>__plt2__ at line 242 column 14
__plt__ at line 107 column 18
plot at line 223 column 10
logistic at line 8 column 1
error: set: "dataaspectratio" must be finite
Here's the full script that I used:
R=linspace(0,4,100);
for j=1:100
r=R(j);
X=linspace(0,1,100);
for i=1:1000
X=r*(X-X.*X);
endfor
plot(R,X);
hold on;
endfor
Just now, after starting Octave again, this problem went away. A while later it came back. All I did was zoom into a plot that I made. The plot window still comes up the first time, but it's just a horizontal line with no axes. After that, the plot window doesn't even come up.

Related

Same command broadcasts OK from command line, but not in a script

I'm relying on broadcasting in the following element-by-element multiplication of a 2x2x2 matrix by a 1x2 matrix (broadcasting means the smaller matrix is repeated as many times as necessary to match the size of the larger one - see end for more explanation). One command gives a "nonconformant arguments" error when in a script run from the octave command line. But exactly the same command runs fine if typed at the octave command line.
Occasionally, the script doesn't give the error, but I haven't worked out exactly what arrangement the Universe has to be in to avoid the error (see later) -- whatever, running it within a script is as robust as a cheap garden strimmer.
I give a workround below, but I'd like to understand what's behind this error.
Here's my script file mwe_bcast.m:
# Minimum working example of broadcasting an element-by-element multiplication.
# The last line errors when run from octave as a script,
# but not when typed at the octave command line.
clear
test = [0,1;2,3];
test(:,:,2) = 3 - test;
test .*= 2;
factors = [1,2;3,4];
test .*= factors(1,:)
But here's the error when run as a script:
>> mwe_bcast
error: mwe_bcast: .*=: nonconformant arguments (op1 is 2x2x2, op2 is 1x2)
error: called from
mwe_bcast at line 9 column 6
Nonetheless, when I then type the last line at the command line there is no error, and the following output shows that the broadcasting has worked as expected:
>> test .*= factors(1,:)
test =
ans(:,:,1) =
0 4
4 12
ans(:,:,2) =
6 8
2 0
Even weirder: I checked whether this is repeatable after restarting octave. It is and it isn't! Sometimes, if I run the script any amount of times straight after starting octave (after just a cd to the appropriate directory), it runs without errors. But as soon as I edit the script in any way (just changing a comment, and sometimes even just opening it in the octave editor), then re-run the script, I get the error. But, other times, I get the error the first time I run the script after restarting octave.
Workround: I get no error from the script if I don't use the abbreviated assignment (.*=) for this broadcast element-by-element multiplication, i.e. replace the last line with:
test = test .* factors(1,:);
However, just as weirdly, note that the earlier use of .*= in the script to multiply the same matrix by a scalar always works without an error, even though it also relies on broadcasting.
Explanation of broadcasting: Essentially, the last line multiplies the matrix
test(:,:,1) = 0,1
2,3
test(:,:,2) = 3,2
1,0
element-by-element by [1,2] which broadcasts as if it were the matrix:
factors(:,:,1) = 1,2
1,2
factors(:,:,2) = 1,2
1,2
System:
GNU Octave, version 5.2.0
Ubuntu 20.04 LTE
Linux 5.15

How do I fix the index error in my Octave code?

I'm having issues with the index for my code. I'm trying to create a code on Octave for the power method (vector iteration) and the error: 'x(4): out of bound 3' keeps popping up at line 6.
A=[6,-2,2,4;0,-4,2,2;0,0,2,-5;0,0,0,-3]
b=[12;10;-9;-3]
n=4
for i=rows(A):-1:1
for j=i+1:rows(A)
x(i)=[b(i)-A(i,j)*x(j)]/A(i,i); #error: 'x(4): out of bound 3'
endfor
endfor
x
In the following line, note that you have x appearing twice; the first seeks to assign to it, but the second simply tries to access its value:
x(i) = [ b(i) - A(i,j) * x(j) ] / A(i,i);
⬑ assignment ⬑ access
Assigning to an index that doesn't exist (yet) is absolutely fine; octave will simply fill in the intervening values with 'zeroes'. E.g.
>> clear x
>> x(3) = 1 % output: x = [0, 0, 1]
However, trying to access an index which doesn't exist yet is an error, since there's nothing there to access. This results in an "out of bound" error (and, in its error message, octave is kind enough to tell you what the last legitimate index is that you can access in that particular array).
Therefore, this is an error:
>> clear x
>> x(3) = 1 % output: x = [0, 0, 1]
>> 1 + x(4) % output: error: x(4): out of bound 3
Now going back to your specific code, you are trying to access something that doesn't exist yet. The reason it doesn't exist yet, is that you have set up your for loops such that j will achieve a higher value than i at a particular step, such that you are trying to access x(j), which does not exist yet, in order to assign it to x(i), where i < j. Therefore this results in an out of bounds error (you are trying to access index j when you only have up to i available).
In your particular case, octave informs you that this happened when j was 4, and i was 3.
PS: I will echo #HansHirse's implied warning here, that you should always pay attention to your variables, and clear them appropriately in your scripts, especially if you plan to run it many times. Never use a variable that you haven't defined (or cleared) beforehand. Otherwise, x here may not be undefined when you run your script, say, a second time. This leads to all sorts of problems, e.g., your code works but for the wrong reasons, and then fails to work again when you run it the next day and x is now undefined. In this particular example, if you had an x in your workspace which had the right number of elements in it, your code would "work" but produce the wrong result, and you wouldn't know any better.

Extracting blocks of data after fix step from Molecular Dynamics

I am newbie in bash programming. My problem is the following:
I have a trajectory file in xyz extension, out of a molecular dynamics calculation. In this file there are, more or less, 2000 blocks of coordinates. Each block of coordinate start with the number of atoms and end with the same number of atoms reported, e.g. of a block:
352 (starting point first block)
i = 0,
...
...
352 (ending point first block/starting point second block)
i = 10,
...
...
352
out of this file, I would like to be able to grep some of the blocks/configurations along the all trajectory file, at a fix ratio, lets say, every 10 configurations ( so that in the end I have 200 sampled structures from the beginning to the end of the trajectory file).
I thought, but again, I am not an expert, to use a for loop like:
for i in .xyz
do (grep "352
\n.*some_pattern.*
\n.*352");
done
The point is that I have no idea on 1st how to tell the script to grep the block and 2nd to extract the blocks every 10 configurations.
Can you kindly help me?
P.S. while for the first point I think after sometime I will be able to fix it, the second is the most annoying.

Set time for one tick mark in cesium

Is there a way to set how much time one tick mark on the time line in Cesium is equal to? I know viewer.timeline.zoomTo() sets the time range for the whole timeline.
There's no public API for this, but if you're OK with a little hacking, you can find this answer in local variable tinyTic in Timeline.js around line 490. The line I've identified here is just a blank line between two sections of code: the code above this line is the last bit of logic to modify tinyTic (possibly resetting it to minSize on line 485), and then on line 493 you can see tic = getNextTic(tic, tinyTic) being used to generate the smallest tics.
You could, for example, add a line of code on L490 that saves the value of tinyTic to some object that you can access from outside code. The value here is measured in seconds (so 5 means 5 seconds, 300 means 5 minutes, etc).

Octave specgram generates two diffent answers

I have a project where I have to recognize the frequency from an audio file. For this I use a single tone of 10 kHz to see if I can get it working.
Since I am pretty new to Octave, I tried this example with my own audio file.
I tried to understand what happens by doing some research to all functions.
My question here is; if I let specgram plot the figure when I do not specify it's output:
specgram(y,fftn,Fs,hanning(window),step);
it gives a line at 10kHz which is what I want.
But if I specify the output for the specgram function
[S,f,t]= specgram(y,fftn,Fs,hanning(window),step);
and let it plot, it plots the line at 18 kHz.
I figured it have to be in the inputs for the figure and I tried modifying these a bit, but every time I do that Octave gives an error.
I need the frequency as an given output, since I have to do some calculations with it, I figured I need to specify the frequency output.
This is the part of the code that specify the plot for the spectrogram:
step= fix(5*Fs/1000); % stepsize of the window
window= fix(90*Fs/1000); % window size
fftn =2^nextpow2(window); % Size of the FFT block
[S,f,t]= specgram(y,fftn,Fs,hanning(window),step);
S= abs(S(2:fftn*12000/Fs,:)); % Normalize the phase
S= S/max(S(:)); % Normalize the Energy
S= max(S, 10^(-40/10)); % Throw out values below -40 dB and above -3dB
S= min(S, 10^(-3/10));
figure
imagesc(t,f,(log(S)));
Can anyone help me here how to gain the frequency data from the audio file so I can use it in some calculations?
I have searched for answers already in the Octave manual for help and I tried it with various matlab sites. Also checked already many posts here such as:
How does Octave spectrogram 'specgram' from signal work?
Methodology of FFT for Matlab spectrogram / short time Fourier transform functions
P.S. Sorry for my bad English, it's not my native language
I found the answer myself, it turns out it is in this line of code:
S= abs(S(2:fftn*12000/Fs,:));
if I delete this line, the lines are placed on the right frequency in the figure. To me it looks like this line just takes a small space of the fft and replaces it with other frequencies but I'm not shure about that.