search for specific word in text file in Matlab - string

I want to search for a specific word in a text file and return its position. This code reads the text fine...
fid = fopen('jojo-1 .txt','r');
while 1
tline = fgetl(fid);
if ~ischar(tline)
break
end
end
but when I add this code
U = strfind(tline, 'Term');
it returns [] although the string 'Term' exists in the file.
Can you please help me?

For me, it works fine:
strfind(' ertret Term ewrwerewr', 'Term')
ans =
9
Are you sure that 'Term' is really in your line?

I believe that your ~ischar(tline) makes the trouble because the code "breaks" when the tline is not char..so the strfind cannot find anything.
so the mayor change I made is to actually search for the String at the line which was identified as a line with some characters.
I tried a little bit modification of your code on my TEXT file:
yyyy/mmdd(or -ddd)/hh.h):2011/-201/10.0UT geog Lat/Long/Alt= 50.0/ 210.0/2000.0
NeQuick is used for topside Ne profile
URSI maps are used for the F2 peak density (NmF2)
CCIR maps are used for the F2 peak height (hmF2)
IRI-95 option is used for D-region
ABT-2009 option is used for the bottomside thickness parameter B0
The foF2 STORM model is turned on
Scotto-97 no L option is used for the F1 occurrence probability
TBT-2011 option is used for the electron temperature
RBY10+TTS03 option is used for ion composition
Peak Densities/cm-3: NmF2= 281323.9 NmF1= 0.0 NmE= 2403.3
Peak Heights/km: hmF2= 312.47 hmF1= 0.00 hmE= 110.00
Solar Zenith Angle/degree 109.6
Dip (Magnetic Inclination)/degree 65.76
Modip (Modified Dip)/degree 55.06
Solar Sunspot Number (12-months running mean) Rz12 57.5
Ionospheric-Effective Solar Index IG12 63.3
TEC [1.E16 m-2] is obtained by numerical integration in 1km steps
from 50 to 2000.0 km. t is the percentage of TEC above the F peak.
-
H ELECTRON DENSITY TEMPERATURES ION PERCENTAGES/% 1E16m-2
km Ne/cm-3 Ne/NmF2 Tn/K Ti/K Te/K O+ N+ H+ He+ O2+ NO+ Clust TEC t/%
0.0 -1 -1.000 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 7.7 75
5.0 -1 -1.000 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 7.7 75
10.0 -1 -1.000 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 7.7 75
it is an output from one Ionospheric model but that is not important:)
so I used following Matlab code to find where it string TEMPERATURES
out = fopen('fort.7'); % Open function
counter = 0; % line counter (sloppy but works)
while 1 % infinite loop
tline = fgetl(out); % read a line
counter = counter + 1; % we are one line further
if ischar(tline) % if the line is string
U = strfind(tline, 'TEMPERATURES'); % where the string start (if at all)
if isfinite(U) == 1; % if it is a number actually
break % we found it, lets go home
end
end
end
results:
counter = 26
U = 27

Related

Different output on Function on assesment (Hackerrank) (FIXED)

So, for this assessment on Hackerrank i need to calculate the ratios on given input. So i coded but whenever iam running this on my own shell it gives me the right output ,but in the compiler from Hackerrank it rounds the results up to a full integer.
Down below my own code. The test input =
6
-4 3 -9 0 4 1
My Code =
def plusMinus(arr):
array = []
positive_int = 0
negative_int = 0
zero_int = 0
total_int = 0
#removing the first int from input & appending the rest of the input to the empty array
for i in arr[1:]: ***Error was in this line of code. Removed the parameter "[1:]" and it gave me the right output***
array.append(i)
total_int = len(array)
#calcuting with every integer in array if positive or negative and counting them
for i in array:
if i >= 1:
positive_int +=1
elif i < 0:
negative_int +=1
elif i == 0:
zero_int +=1
#calculate the ratio of the negatives and positives integers
pos_ratio = positive_int / total_int
negative_ratio = negative_int / total_int
zero_ratio = zero_int / total_int
#format to six decimal places
formatted_pos_ratio = "{:.6f}".format(pos_ratio)
formatted_negative_ratio = "{:.6f}".format(negative_ratio)
formatted_zero_ratio = "{:.6f}".format(zero_ratio)
#printing out the ratio's
print(str(formatted_pos_ratio))
print(str(formatted_negative_ratio))
print(str(formatted_zero_ratio))
Output in Hackerrank Compiler =
0.600000
0.200000
0.200000
and in my own compiler its:
0.500000
0.333333
0.166667
What am i doing wrong? (Bear in mind, i just started coding so my code is most probably not the best way to solve this.)

Finding the second largest number?

this program is to find the second largest number this works for most of the inputs but it is not working for the following inputs.
n=4
a1 = '-7 -7 -7 -7 -6'
a1=[int(arr_temp) for arr_temp in a1.strip().split(' ')]
print(a1)
largest = max(a1)
largest2 = 0
for i in range(0,len(a1)):
if ((a1[i]>largest2 or a1[i]<0) and largest2<largest and a1[i]!=largest):
largest2 = a1[i]
print(largest2)
Setting largest2 to 0 just complicates the if statement later. Set it to the smallest value in the array and it becomes clearer.
n=4
a1 = '-7 -7 -7 -7 -6'
a1=[int(arr_temp) for arr_temp in a1.strip().split(' ')]
print(a1)
largest = max(a1)
largest2 = min(a1)
for i in range(0,len(a1)):
if (a1[i] > largest2) and (a1[i] < largest):
largest2 = a1[i]
print(largest2)
Note that if the array is large, the call to min becomes non-trivial. In that case you could set largest2 to the smallest value possible (on that note, this link might be useful)

Taxicab geometry task

So, i've been struggling for some time with this task. It sounds like this: given N points(X,Y) X,Y integers, and M questions of the form P(A, B), find the total distance from point P(A,B) to all the N given points. Distance from A(x1, y1) to B(x2, y2) = max(|x1-x2|, |y1-y2|). Maybe it sounds wierd, i'm not an english speaker, sorry for the mistakes. I'll leave here the IN/OUT
IN.txt (N = 4, M = 3, the first 4 coordinates represent the given points.
the next 3 coordinates are the points from which i have to compute the total lenght)
4 3
3 5
-3 -2
1 4
-4 -3
2 -4
1 4
4 2
OUT.txt
28
15
21
Here's some Python that should do the trick for you. Be sure to pay attention to which directory you're in when you're writing so you don't overwrite things.
I've tested it on the input information you presented in the question, and it works, providing the formatted output file as desired.
# Assuming you're in the directory to IN.txt -- otherwise, insert the filepath.
input_file = open("IN.txt", "r")
# Read the input file and split it by new lines
input_lines_raw = input_file.read().split('\n')
input_file.close()
# Split the input lines and eliminate the spaces/create the vector int lists
input_lines_split = []
for element in input_lines_raw:
input_lines_split.append(element.split(' '))
input_lines = []
for sub in input_lines_split:
inserter = []
for elem in sub:
if (len(elem) > 0):
inserter.append(elem)
input_lines.append(inserter)
input_lines = [[int(j) for j in i] for i in input_lines]
# Build the original and final vector arrays
origin_vectors = []
dest_vectors = []
for i in range(1, input_lines[0][0] + 1):
dest_vectors.append(input_lines[i])
for i in range(input_lines[0][0] + 1, input_lines[0][0] + input_lines[0][1] + 1):
origin_vectors.append(input_lines[i])
# "Distance" operations on the lists of vectors themselves/generate results array
results_arr = []
for original in origin_vectors:
counter = 0
for final in dest_vectors:
counter = counter + max(abs(original[0] - final[0]), abs(original[1] - final[1]))
results_arr.append(counter)
print(results_arr)
for element in results_arr:
print(str(element))
# Open the ouput file and write to it, creating a new one if it doesn't exist.
# NOTE: This will overrwrite any existing "OUT.txt" file in the current directory.
output_file = open("OUT.txt", "w")
for element in results_arr:
output_file.write(str(element) + '\n')
output_file.close()

Creating and modifying arbitrary-length arrays while plotting in gnuplot

I would like to count the number of occurrences of an event (for example, x data value equals some number) and store these occurrences in order, while plotting a file in gnuplot. Say I have the following file:
1
0
0
0
1
1
0
Now I want to count how many times I have a 1 and store that number in variable N. Then I want to know the positions where that happens and store that information in an array pos, all of this while plotting the file. The result, for the example above, should be:
print N
3
print pos
1 5 6
I know how to achieve the counting:
N = 0
plot "data" u ($0):($1 == 1 ? (N = N+1, $1) : $1)
print N
3
Then to achieve the position recording, it would be schematically something like this:
N = 0 ; pos = ""
plot "data" u ($0):($1 == 1 ? (N = N+1, pos = pos." ".$0, $1) : $1) # This doesn't work!
print N
3
print pos
1 5 6
How can this be done in gnuplot without resorting to external bash commands?
Well, as sometimes happens writing down the question triggers an idea for an answer. I'll leave it here in case somebody finds it useful:
N=0 ; pos=""
plot "data" u ($0):($1 == 1 ? (N = N+1, pos = sprintf("%s %g", pos, $0+1), $1) : $1)
print N
3
print pos
1 5 6
Note I had to use $0+1 because position 1 is treated by gnuplot as zero.

Can gnuplot compute and plot the delta between consecutive data points

For instance, given the following data file (x^2 for this example):
0
1
4
9
16
25
Can gnuplot plot the points along with the differences between the points as if it were:
0 0
1 1 # ( 1 - 0 = 1)
4 3 # ( 4 - 1 = 3)
9 5 # ( 9 - 4 = 5)
16 7 # (16 - 9 = 7)
25 9 # (25 -16 = 9)
The actual file has more than just the column I'm interested in and I would like to avoid pre-processing in order to add the deltas, if possible.
dtop's solution didn't work for me, but this works and is purely gnuplot (not calling awk):
delta_v(x) = ( vD = x - old_v, old_v = x, vD)
old_v = NaN
set title "Compute Deltas"
set style data lines
plot 'data.dat' using 0:($1), '' using 0:(delta_v($1)) title 'Delta'
Sample data file named 'data.dat':
0
1
4
9
16
25
Here's how to do this without pre-processing:
Script for gnuplot:
# runtime_delta.dem script
# run with
# gnuplot> load 'runtime_delta.dem'
#
reset
delta_v(x) = ( vD = x - old_v, old_v = x, vD)
old_v = NaN
set title "Compute Deltas"
set style data lines
plot 'runtime_delta.dat' using 0:(column('Data')), '' using 0:(delta_v(column('Data'))) title 'Delta'
Sample data file 'runtime_delta.dat':
Data
0
1
4
9
16
25
How about using awk?
plot "< awk '{print $1,$1-prev; prev=$1}' <datafilename>"
Below is a version that uses arrays from Gnuplot 5.1. Using arrays allows multiple diffs to be calculated in single Gnuplot instance.
array Z[128]
do for [i=1:128] { Z[i] = NaN }
diff(i, x) = (y = x - Z[i], Z[i] = x, y)
i is the instance index that needs to be incremented for each use. For example
plot "file1.csv" using 1:(diff(1,$2)) using line, \
"file2.csv" using 1:(diff(2,$2)) using line

Resources