Save data in octave - octave

In Octave, I have x=-13:0.1:13
then, I save as:
save file.dat x
and when I open file.dat I get:
-13
-12.9
-12.8
-12.7
-12.6
-12.5
-12.4
-12.3
-12.2
-12.1
-12
-11.9
-11.8
-11.7
-11.6
-11.5
-11.4
-11.3
-11.2
-11.1
-11
-10.9
-10.8
-10.7
-10.6
-10.5
-10.4
-10.3
-10.2
-10.1
-10
-9.9
-9.800000000000001
-9.699999999999999
-9.6
-9.5
-9.4
-9.300000000000001
-9.199999999999999
-9.1
-9
-8.899999999999999
-8.800000000000001
-8.699999999999999
...
But I would like to save -8.7, and not -8.699999999999999; -8.8 and not -8.800000000000001...

Without knowing what function you're using to write to the file, one way to fix the problem is specify the format with single digit precision.
For example:
fprintf(fid, "%4.1f", x(i));
writes x(i) to fid with 1 digit after the decimal.

Related

Python OCR Tesseract, find a certain word in the image and return me the coordinates

I wanted your help, I've been trying for a few months to make a code that finds a word in the image and returns the coordinates where that word is in the image.
I was trying this using OpenCV, OCR tesseract, but I was not successful, could someone here in the community help me?
I'll leave an image here as an example:
Here is something you can start with:
import pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = r'C:\<path-to-your-tesseract>\Tesseract-OCR\tesseract.exe'
img = Image.open("img.png")
data = pytesseract.image_to_data(img, output_type='dict')
boxes = len(data['level'])
for i in range(boxes):
if data['text'][i] != '':
print(data['left'][i], data['top'][i], data['width'][i], data['height'][i], data['text'][i])
If you have difficulties with installing pytesseract see: https://stackoverflow.com/a/53672281/18667225
Output:
153 107 277 50 Palavras
151 197 133 37 com
309 186 154 48 R/RR
154 303 126 47 Rato
726 302 158 47 Resto
154 377 144 50 Rodo
720 379 159 47 Arroz
152 457 160 48 Carro
726 457 151 46 Ferro
154 532 142 50 Rede
726 534 159 47 Barro
154 609 202 50 Parede
726 611 186 47 Barata
154 690 124 47 Faro
726 685 288 50 Beterraba
154 767 192 47 Escuro
726 766 151 47 Ferro
I managed to find the solution and I'll post it here for you:
import pytesseract
import cv2
from pytesseract import Output
pytesseract.pytesseract.tesseract_cmd = r'C:\<path-to-your-tesseract>\Tesseract-OCR\tesseract.exe'
filepath = 'image.jpg'
image = cv2.imread(filepath, 1)
# converting image to grayscale image
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# converting to binary image by Thresholding
# this step is necessary if you have a color image because if you skip this part
# then the tesseract will not be able to detect the text correctly and it will give an incorrect result
threshold_img = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
# displays the image
cv2.imshow('threshold image', threshold_img)
# Holds the output window until the user presses a key
cv2.waitKey(0)
# Destroying windows present on the screen
cv2.destroyAllWindows()
# setting parameters for tesseract
custom_config = r'--oem 3 --psm 6'
# now feeding image to tesseract
details = pytesseract.image_to_data(threshold_img, output_type=Output.DICT, config=custom_config, lang='eng')
# Color
vermelho = (0, 0, 255)
#Exibe todas as chaves encontradas
print(details.keys())
print(details['text'])
# For in all found texts
for i in range(len(details['text'])):
# If it finds the text "UNIVERIDADE" it will print the coordinates, and draw a rectangle around the word
if details['text'][i] == 'UNIVERSIDADE':
print(details['text'][i])
print(f"left: {details['left'][i]}")
print(f"top: {details['top'][i]}")
print(f"width: {details['width'][i]}")
print(f"height: {details['height'][i]}")
cv2.rectangle(image, (details['left'][i], details['top'][i]), (details['left'][i]+details['width'][i], details['top'][i]+details['height'][i]), vermelho)

Calling .csv file into Octave

I have a code written in C++ that outputs a .csv file with data in three columns (Time, Force, Height). I want to plot the data using Octave, or else use the octave function plot in the C++ file (I'm aware this is possible but I don't necessarily need to do it this way).
Right now I have the simple .m file:
filename = linear_wave_loading.csv;
M = csvread(filename);
Just to practice bringing this file into octave (will try and plot after)
I am getting this error.
error: dlmread: error parsing range
What is the correct method to load .csv files into octave?
Edit: Here is the first few lines of my .csv file
Wavelength= 88.7927 m
Time Height Force(KN/m)
0 -20 70668.2
0 -19 65875
0 -18 61411.9
0 -17 57256.4
Thanks in advance for your help.
Using octave 3.8.2
>> format long g
>> dlmread ('test.csv',' ',2,0)
ans =
0 0 0 -20 70668.2
0 0 0 -19 65875
0 0 0 -18 61411.9
0 0 0 -17 57256.4
General, use dlmread if your value separator is not a comma. Furthermore, you have to skip the two headlines.
Theoretical dlmread works with tab separated values too '\t', but this failes with you given example, because of the discontinuous tab size (maybe it's just a copy paste problem), so taking one space ' ' as separator is a workaround.
you should better save your .csv file comma separated
Wavelength= 88.7927 m
Time Height Force(KN/m)
0, -20, 70668.2
0, -19, 65875
0, -18, 61411.9
0, -17, 57256.4
Then you can easily do dlmread('file.csv',',',2,0).
You can try my csv2cell(not to be confused with csv2cell from io package!) function (never tried it < 3.8.0).
>> str2double(reshape(csv2cell('test.csv', ' +',2),3,4))'
ans =
0 -20 70668.2
0 -19 65875
0 -18 61411.9
0 -17 57256.4
Usually it reshaped successful automatically, but in case of space seperators, it often failed, so you have to reshape it by your self (and convert to double in any case).
And when you need your headline
>> reshape(csv2cell('test.csv', ' +',1),3,5)'
ans =
{
[1,1] = Time
[2,1] = +0
[3,1] = +0
[4,1] = +0
[5,1] = +0
[1,2] = Height
[2,2] = -20
[3,2] = -19
[4,2] = -18
[5,2] = -17
[1,3] = Force(KN/m)
[2,3] = 70668.2
[3,3] = 65875
[4,3] = 61411.9
[5,3] = 57256.4
}
But take care, then everything is a string in your cell.
Your not storing you .csv filename as a string.
Try:
filename = 'linear_wave_loading.csv';

Problems using "AND" Logical Expression for defining a Mapserver class

I can't seem to get past this hurdle. Mapserver isn't throwing any errors...but it isn't returning anything either...I suspect my logical expression (... in the absence of any errors...I really have little clue what is going down here).
Ideally, I'd like to filter by my shapefile using these two columns: '[YODA] (text)' AND '[ZOOM] (Integer)'.
Currently my code reads as:
LAYER
# Zoom Level 11-16
TYPE ANNOTATION
STATUS ON
GROUP "yoda"
DATA "yoda_graphics"
NAME "yoda_awesome"
# # Visible in map from zoom level 11 onwards
MAXSCALEDENOM 325008
MINSCALEDENOM 5078
LABELITEM "label"
CLASS
# Yoda Head
EXPRESSION (('[YODA]' ~* '/^I/') AND ([Zoom]>8)) ## where things are suspect...
# yoda shell symbol w/ label
STYLE
SYMBOL 'yoda_red_top_shell'
#COLOR 255 255 255
#COLOR 218 218 203
COLOR 184 184 156
SIZE 16
END
STYLE
SYMBOL 'yoda_red_top_shell'
#COLOR 225 104 104
#COLOR 204 184 181
COLOR 214 214 169
SIZE 15
END
STYLE
SYMBOL 'yoda_blue_shell'
#COLOR 80 101 123
#COLOR 183 192 221
COLOR 241 241 226
SIZE 15
END
LABEL
TYPE truetype
FONT "deja-bold"
SIZE 5
#COLOR 255 255 255
COLOR 184 184 156
PARTIALS FALSE
WRAP " "
ALIGN center
POSITION CC
ANGLE 0
END # end label
END #end class
END # layer
You shouldn't surround your regular expression with slashes when using an explicit regular expression operator.
This is correct:
CLASSITEM "Yoda"
CLASS
EXPRESSION /^I/
In your case, use:
EXPRESSION (('[YODA]' ~* '^I') AND ([Zoom]>8))

Create line chart from csv file without excel

Suppose i have the following data in csv format :
Time Total Allocated Deallocated
0.00004 0 16 0
0.000516 16 31 0
0.046274 47 4100 0
0.047036 4147 0 31
0.047602 4116 35 0
0.214296 4151 4100 0
0.215109 8251 0 35
i am looking for some kind of software that will allow me to make a line chart of it ( where time column will be the X axis) , i used excel for now , but i am looking for something else,that will allow me to see in greater details .
Any ideas ?
Use Datawrapper. It's very easy and you can publish it on the web or export it to a PNG file.
You can also use R. Here is an example of code to generate a time series plot :
library("ggplot2")
df <- data.frame(date = seq(as.Date("2012-01-01"),as.Date("2012-12-01"), by = "month"), x = rnorm(12))
ggplot(df, aes(x=date, y = x)) + geom_line() + theme_bw()
This is an old question but still: https://plot.ly is also a good site for that kind of stuff.

Find the source of an Haskell exception

I am trying to figure out where an exception is thrown. These are my first experiences with exception handling in Haskell. I am trying to call an XML-RPC function on a remote host, that is accessed using https:
ghci> import Network.XmlRpc.Client
ghci> import Network.XmlRpc.Internals
ghci> remote "https://rpc.ote.gandi.net/xmlrpc/" "domain.count" (ValueString "01234567890ABCDEF")
*** Exception: user error (https not supported)
In order to figure out if I just forgot to enable SSL support in some package or if it's something different, I would like to know which package throws the exception.
I started by following the instructions in the GHC docs, but it is not working out as expected:
ghci> :set -fbreak-on-exception
ghci> :trace remote "https://rpc.ote.gandi.net/xmlrpc/" "domain.count" (ValueString "01234567890ABCDEF")
Stopped at <exception thrown>
_exception :: e = _
ghci> :hist
Empty history. Perhaps you forgot to use :trace?
All relevant packages should be compiled with --enable-library-profiling.
How to locate the exception?
The reason you couldn't get any good information out of it is that :trace can't go into library code -- we need to interpret any code we want to trace. Whether it was compiled with profiling is irrelevant. After installing some dependencies, I did this to get some more information:
% cabal unpack haxr
% cd haxr-3000.8.5
% ghci Network/XmlRpc/Client.hs -XOverlappingInstances -XTypeSynonymInstances -XFlexibleInstances -XTemplateHaskell
*Network.XmlRpc.Client> :set -fbreak-on-exception
*Network.XmlRpc.Client> :trace remote "https://rpc.ote.gandi.net/xmlrpc/" "domain.count" (ValueString "01234567890ABCDEF")
Stopped at <exception thrown>
_exception :: e = _
[<exception thrown>] *Network.XmlRpc.Client> :hist
-1 : authHdr (Network/XmlRpc/Client.hs:169:27-33)
-2 : request:parseUserInfo (Network/XmlRpc/Client.hs:161:34-40)
-3 : request:parseUserInfo (Network/XmlRpc/Client.hs:161:31-73)
-4 : request:parseUserInfo:(...) (Network/XmlRpc/Client.hs:159:55-70)
-5 : request:parseUserInfo:(...) (Network/XmlRpc/Client.hs:159:39-51)
-6 : request:parseUserInfo:(...) (Network/XmlRpc/Client.hs:159:39-70)
-7 : request:parseUserInfo (Network/XmlRpc/Client.hs:160:34-39)
-8 : request:parseUserInfo (Network/XmlRpc/Client.hs:160:31-64)
-9 : request:parseUserInfo (Network/XmlRpc/Client.hs:(160,29)-(161,74))
-10 : request:parseUserInfo (Network/XmlRpc/Client.hs:(159,5)-(161,74))
-11 : authHdr (Network/XmlRpc/Client.hs:(169,1)-(175,60))
-12 : request:headers (Network/XmlRpc/Client.hs:158:33-47)
-13 : request:headers (Network/XmlRpc/Client.hs:158:33-63)
-14 : request:headers (Network/XmlRpc/Client.hs:158:33-70)
-15 : request:headers (Network/XmlRpc/Client.hs:158:20-71)
-16 : request:headers (Network/XmlRpc/Client.hs:157:16-65)
-17 : request:headers (Network/XmlRpc/Client.hs:156:16-47)
-18 : request:headers (Network/XmlRpc/Client.hs:155:16-44)
-19 : request:headers (Network/XmlRpc/Client.hs:(155,15)-(158,71))
-20 : request (Network/XmlRpc/Client.hs:(149,28)-(152,54))
...
Hopefully that gets you started. You may find that this leads you to another library boundary -- if so, you'll need to unpack and interpret that library to go deeper. Good luck!