Octave puts file read by dlmread to console - octave

myFile.asc is a large point cloud with million of points (rows) in the form of
-13.204000000 30.858300000 15.898300000
-13.204300000 30.864600000 15.899400000
-13.195000000 30.857300000 15.901000000
P = dlmread('/myPath/myFile.asc', " ")
stores the pointcloud successfully in P but unfortunately gives each line to console out in Octave. How can I suppress the logging here?

You can add a semicolon at the end to suppress the output:
P = dlmread('/myPath/myFile.asc', ' ');

Related

Python 3 psycopg2 COPY from stdin failed: error in .read()

I am trying to apply the code found on this page, in particular part 'Copy Data from String Iterator' of the Table of Contents, but run into an issue with my code.
Since not all lines coming from the generator (here log_lines) can be imported into the PostgreSQL database, I try to filter the correct lines (here row) using itertools.filterfalse like in the codeblock below:
def copy_string_iterator(connection, log_lines) -> None:
with connection.cursor() as cursor:
create_staging_table(cursor)
log_string_iterator = StringIteratorIO((
'|'.join(map(clean_csv_value, (
row['date'],
row['time'],
row['cs_uri_query'],
row['s_contentpath'],
row['sc_status'],
row['s_computername'],
...
row['sc_substates'],
row['s_port'],
row['cs_version'],
row['c_protocol'],
row.update({'cs_cookie':'x'}),
row['timetakenms'],
row['cs_uri_stem'],
))) + '\n')
for row in filterfalse(lambda line: "#" in line.get('date'), log_lines)
)
cursor.copy_from(log_string_iterator, 'log_table', sep = '|')
When I run this, cursor.copy_from() gives me the following error:
QueryCanceled: COPY from stdin failed: error in .read() call
CONTEXT: COPY log_table, line 112910
I understand why this error happens, it is because in the test file I use there are only 112909 lines that meet the filterfalse condition. But why does it try to copy line 112910 and throw the error and not just stop?
Since Python doesn't have a coalescing operator, add something like:
(map(clean_csv_value, (
row['date'] if 'date' in row else None,
:
row['cs_uri_stem'] if 'cs_uri_stem' in row else None,
))) + '\n')
for each of your fields so you can handle any missing fields in the JSON file. Of course the fields should be nullable in the db if you use None otherwise replace with None with some default value for that field.

Python csv data logging doesn't work in while loop

I have been trying to log the data received from the Arduino through USB port and the strange thing is that the code works on my mac just fine but on windows it won't write it. At the start I expected the initial writing "DATA" but it didn't even write that. And when I commented out the entire loop it worked (It says "DATA" in the csv file).
import serial
count = 1
port = serial.Serial('COM4', baudrate=9600, bytesize=8)
log = open("data_log.csv", "w")
log.write("DATA")
log.write("\n")
while 1:
value = str(port.read(8), 'utf-8')
value = value.replace('\r', '').replace('\n', '')
if value.strip():
log.write(str(count))
log.write(',')
log.write(value)
log.write('\n')
print(count)
count += 1
print(value)
\n = CR (Carriage Return) // Used as a new line character in Unix
\r = LF (Line Feed) // Used as a new line character in Mac OS
\n\r = CR + LF // Used as a new line character in Windows
I think it's not working in windows because you need to look for a CR LF.
Might try using Environment.NewLine as it will act as any of the above depending on the operating system.

I couldn't find the solution on "IndentationError"

def duty2():
numbers = []
while True:
a = Input('Enter a new number, 0 to end: ')
if a == 0:
break
numbers.append(a)
if len(numbers)!=0:
sums = 0
for i in numbers:
sums = sums + i
average = float(sums) / len(numbers)
print "The average of %s is %.2f" % (numbers, average)
else:
print "There is nothing to calculate."
I'm new at coding, I could'n solve the problem please help
**I am getting this error " IndentationError: unindent does not match any outer indentation level*
**
You have an extra space in front of the line that reads numbers.append(a)
When you run the code (I've thrown it into the file tmp.py), it'll tell you exactly which line is causing the issue. For example, when I run your code I get the following:
File "tmp.py", line 8
numbers.append(a)
^
IndentationError: unindent does not match any outer indentation level
This tells me there is an indentation error, that it's on line 8 and it even tells me exactly which line is causing the error.

Getting user inputs from the STDIN in Octave using input()?

Can any please help me why am I getting this error on running the Octave(version 3.8.1) code below-
a = input("");
b = input("");
printf("%d", a+b);
./CandidateCode.m: line 1: syntax error near unexpected token ('
./CandidateCode.m: line 1:a = input("");'
Please help me in resolving this error.
If you run your Script CandidateCode.m from shell, you have to add an interpreter with shebang:
Your CandidateCode.m:
#!/usr/bin/octave -q
a = input("");
b = input("");
printf("%d", a+b);
If you want to run it from within Octave, just execute "CandidateCode" (without ./ and .m)

If error messages echo line content

I try to capture lines with calculations in my text document
and execute them.
I use this in my function:
for i in range(startline,endline)
let calculation = getline(i)
...
let out = eval(calculation)
...
endfor
sometimes something goes wrong and I receive this message:
Error detected while processing function....
Line ...
E488: Trailing Characters
Line .. is the line-nr in my function.
I would like to know also which calculation it concerns (which line in my text doc):
If Error detected = echo calculation
How can I check if there is an error message and echo the variable "calculation"?
There are two ways to handle script errors inside a function:
The first is suppressing the error via :silent!. Two downsides: You have to manually check for success, and any normal output from the evaluated script is suppressed, too (unless you do contortions with :unsilent).
let v:errmsg = ''
silent! let out = eval(calculation)
if v:errmsg != ''
" error
endif
I would recommend the second way via try...catch, which avoids the issues with the output and having to explicitly check for an error:
try
let out = eval(calculation)
catch /^Vim\%((\a\+)\)\=:E/
" v:exception contains what is normally in v:errmsg, but with extra
" exception source info prepended, which we cut away.
let v:errmsg = printf("Line: %d\nCalculation: %s\nError: %s", i, calculation, substitute(v:exception, '^Vim\%((\a\+)\)\=:', '', ''))
echohl ErrorMsg
echomsg v:errmsg
echohl None
endtry