how to get the number from a string in matlab - string

I want to how know to get certain numbers from a string in matlab. For example, I have a string:
'ABCD_01 36_00 3 .txt', (there is spacing between 01 and 36)
What I need is to get the number 36 and 3. How can I do it in matlab? I've tried finding the answer from previous posts but can not find one that fits this purpose. Thanks for the help.

Regular expressions:
>> str = 'ABCD_01 36_00 3 .txt';
>> t = str2double( regexp(str,'.* (\d+)_.* (\d+)','tokens','once') )
t =
36 3

If the filenames always start with four characters you can do:
>> filename = 'ABCD_01 36_00 3 .txt';
>> sscanf(filename, '%*4c_%*u %u_%*u %u.txt')
ans =
36
3

Related

Pattern Matching BASIC programming Language and Universe Database

I need to identify following patterns in string.
- "2N':'2N':'2N"
- "2N'-'2N'-'2N"
- "2N'/'2N'/'2N"
- "2N'/'2N'-'2N"
AND SO ON.....
basically i want this pattern if written in Simple language
2 NUMBERS [: / -] 2 NUMBERS [: / -] 2 NUMBERS
So is there anyway by which i could write one pattern which will cover all the possible scenarios ? or else i have to write total 9 patterns and had to match all 9 patterns to string.... and it is not the scenario in my code , i have to match 4, 2 number digits separated by [: / -] to string for which i have towrite total 27 patterns. So for understanding purpose i have taken 3 ,2 digit scenario...
Please help me...Thank you
Maybe you could try something like (Pick R83 style)
OK = X MATCH "2N1X2N1X2N" AND X[3,1]=X[6,1] AND INDEX(":/-",X[3,1],1) > 0
Where variable X is some input string like: 12-34-56
Should set variable OK to 1 if validation passes, else 0 for any invalid format.
This seems to get all your required validation into a single statement. I have assumed that the non-numeric characters have to be the same. If this is not true, the check could be changed to something like:
OK = X MATCH "2N1X2N1X2N" AND INDEX(":/-",X[3,1],1) > 0 AND INDEX(":/-",X[6,1],1) > 0
Ok, I guess the requirement of surrounding characters was not obvious to me. Still, it does not make it much harder. You just need to 'parse' the string looking for the first (I assume) such pattern (if any) in the input string. This can be done in a couple of lines of code. Here is a (rather untested ) R83 style test program:
PROMPT ":"
LOOP
LOOP
CRT 'Enter test string':
INPUT S
WHILE S # "" AND LEN(S) < 8 DO
CRT "Invalid input! Hit RETURN to exit, or enter a string with >= 8 chars!"
REPEAT
UNTIL S = "" DO
*
* Look for 1st occurrence of pattern in string..
CARDNUM = ""
FOR I = 1 TO LEN(S)-7 WHILE CARDNUM = ""
IF S[I,8] MATCH "2N1X2N1X2N" THEN
IF INDEX(":/-",S[I+2,1],1) > 0 AND INDEX(":/-",S[I+5,1],1) > 0 THEN
CARDNUM = S[I,8] ;* Found it!
END ELSE I = I + 8
END
NEXT I
*
CRT CARDNUM
REPEAT
There is only 7 or 8 lines here that actually look for the card number pattern in the source/test string.
Not quite perfect but how about 2N1X2N1X2N this gets you 2 number followed by 1 of any character followed by 2 numbers etc.
This might help:
BIG.STRING ="HELLO TILDE ~ CARD 12:34:56 IS IN THIS STRING"
TEMP.STRING = BIG.STRING
CONVERT "~:/-" TO "*~~~" IN TEMP.STRING
IF TEMP.STRING MATCHES '0X2N"~"2N"~"2N0X' THEN
FIRST.TILDE.POSN = INDEX(TEMP.STRING,"~",1)
CARD.STRING = BIG.STRING[FIRST.TILDE.POSN-2,8]
PRINT CARD.STRING
END

Formating %e output of sprintf

Is it possible to format the output of sprintf, like following or should I use another function.
Say I have an variable dt= 9.765625e-05 and I want use sprintf to make a string for use when saving say a figure
fig = figure(nfig);
plot(x,y);
figStr = sprintf('NS2d_dt%e',dt);
saveas(fig,figStr,'pdf')
The punctuation mark dot presents me with problems, some systems mistake the format of the file.
using
figStr = sprintf('NS2d_dt%.2e',dt);
then
figStr = NS2d_dt9.77e-05
using
figStr = sprintf('NS2d_dt%.e',dt);
then
figStr = NS2d_dt1e-04
which is not precise enough. I would like something like this
using
figStr = sprintf('NS2d_dt%{??}e',dt);
then
figStr = NS2d_dt9765e-08
Essentially the only way to get your desired output is with some manipulation of the value or strings. So here's two solutions for you first with some string manipulation and second by manipulating the value. Hopefully, these 2 approaches will help reason out solutions for other problems, particularly the number manipulation.
String Manipulation
Solution
fmt = #(x) sprintf('%d%.0fe%03d', (sscanf(sprintf('%.4e', x), '%d.%de%d').' .* [1 0.1 1]) - [0 0.5 3]);
Explanation
First I use sprintf to print the number in a defined format
>> sprintf('%.4e', dt)
ans =
9.7656e-05
then sscanf to read it back in making sure to remove the . and e
>> sscanf(sprintf('%.4e', dt), '%d.%de%d').'
ans =
9 7656 -5
before printing it back we perform some manipulation of the data to get the correct values for printing
>> (sscanf(sprintf('%.4e', dt), '%d.%de%d').' .* [1 0.1 1]) - [0 0.5 3]
ans =
9 765.1 -8
and now we print
>> sprintf('%d%.0fe%03d', (sscanf(sprintf('%.4e', dt), '%d.%de%d').' .* [1 0.1 1]) - [0 0.5 3])
ans =
9765e-08
Number Manipulation
Solution
orderof = #(x) floor(log10(abs(x)));
fmt = #(x) sprintf('%.0fe%03d', x*(10^(abs(orderof(x))+3))-0.5, orderof(x)-3);
Explanation
First I create an anonymous orderof function which tells me the order (the number after e) of the input value. So
>> dt = 9.765625e-05;
>> orderof(dt)
ans =
-5
Next we manipulate the number to convert it to a 4 digit integer, this is the effect of adding 3 in
>> floor(dt*(10^(abs(orderof(dt))+3)))
ans =
9756
finally before printing the value we need to figure out the new exponent with
>> orderof(x)-3
ans =
-8
and printing will give us
>> sprintf('%.0fe%03d', floor(dt*(10^(abs(orderof(dt))+3))), orderof(dt)-3)
ans =
9765e-08
Reading your question,
The punctuation mark dot presents me with problems, some systems mistake the format of the file.
it seems to me that your actual problem is that when you build the file name using, for example
figStr = sprintf('NS2d_dt%.2e',dt);
you get
figStr = NS2d_dt9.77e-05
and, then, when you use that string as filename, the . is intepreted as the extension and the .pdf is not attached, so in Explorer you can not open the file double-clicking on it.
Considering that changing the representation of the number dt from 9.765e-05 to 9765e-08 seems quite wierd, you can try the following approach:
use the print function to save your figure in .pdf
add .pdf in the format specifier
This should allows you the either have the right file extension and the right format for the dt value.
peaks
figStr = sprintf('NS2d_dt_%.2e.pdf',dt);
print(gcf,'-dpdf', figStr )
Hope this helps.
figStr = sprintf('NS2d_dt%1.4e',dt)
figStr =
NS2d_dt9.7656e-05
specify the number (1.4 here) as NumbersBeforeDecimal (dot) NumbersAfterDecimal.
Regarding your request:
A = num2str(dt); %// convert to string
B = A([1 3 4 5]); %// extract first four digits
C = A(end-2:end); %// extract power
fspec = 'NS2d_dt%de%d'; %// format spec
sprintf(fspec ,str2num(B),str2num(C)-3)
NS2d_dt9765e-8

convert input to string

How to convert numbers to the first letters of the alphabet? . I want to use this code for me. But I need alphabets for 10,11,12,13 etc. for example if the user will enter 10 , the program will print j for 11 -->"k" . How cam I do this.
My code is same in the link above
You can use this BASH function:
cnvt() { printf "\x$(printf '%x' $((97 + $1 -1)))\n"; }
Test it:
cnvt 10
j
cnvt 11
k
cnvt 26
z
You can use ASCII table for this.
If user inputs 10 you can add 87 and get "a" = 97.
This way input 11 will get a value of "b" = 98.

Parsing a string in MATLAB

I have a 32 bit string that I want to parse into 8 bits each. Then I want to change the 8 bit binary into a single integer, for example:
str = '00000001000000100000001100000100'
output = '1 2 3 4'
I know to use bin2dec, but I'm having difficulty parsing the string.
In Matlab every string is a matrix, so you can use this property. If 8 bits belong to one byte, reshape your data to have one byte per row:
reshape(str,8,[]).'
Doing so, you can apply bin2dec to get the output:
output=bin2dec(reshape(str,8,[]).')
This returns a vetor [1;2;3;4], use num2str(output.') if you want a char array instead.
Another possibility:
>> 2.^(7:-1:0)*reshape(str-'0',8,[])
>> ans =
1 2 3 4
Of course, apply num2str if you need the output in the form of a string.
A more esoteric way:
>> fliplr(typecast(uint32(2.^(31:-1:0)*(str-'0').'),'uint8'))
>> ans =
1 2 3 4
Seeing as how Ben Voigt didn't provide an answer to this question even though he pretty much answered the question in the comments, I will provide one for closure. As he said, you can segment your string into 8 characters each. Strings are essentially an array of characters. As such, split up your string into 8 characters each, then apply bin2dec on each of the strings.
str = '00000001000000100000001100000100';
byte1 = bin2dec(str(1:8));
byte2 = bin2dec(str(9:16));
byte3 = bin2dec(str(17:24));
byte4 = bin2dec(str(25:32));
output = num2str([byte1 byte2 byte3 byte4]);
>> output
output =
1 2 3 4
Looking at your example output, you desire output to be a string, and thus the num2str call in the last line of the code.

Code Golf: Numeric equivalent of an Excel column name

Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
The challenge
The shortest code by character count that will output the numeric equivalent of an Excel column string.
For example, the A column is 1, B is 2, so on and so forth. Once you hit Z, the next column becomes AA, then AB and so on.
Test cases:
A: 1
B: 2
AD: 30
ABC: 731
WTF: 16074
ROFL: 326676
Code count includes input/output (i.e full program).
Excel, 9 chars :)
Use the right tool for the job:
=COLUMN()
Perl, 36 34 33 31 30 17 15 11 characters
$_=()=A..$_
Usage:
$ echo -n WTF | perl -ple '$_=()=A..$_'
16074
Reduced to 17 by using echo -n to avoid a chop call.
Reduced to 15 by using say instead of print.
Reduced to 11 by using -p instead of say.
Explanation:
A is evaluated in string context and A..$_ builds a list starting at "A" and string-incrementing up to the input string. Perl interprets the ++ operator (and thus ..) on strings in an alphabetic context, so for example $_="AZ";$_++;print outputs BA.
=()= (aka "goatse" operator) forces an expression to be evaluated in list context, and returns the number of elements returned by that expression i.e., $scalar = () = <expr> corresponds to #list = <expr>; $scalar = #list.
J, 17 12 10 characters
26#.64-~av
Example:
26#.64-~av 'WTF'
16074
Explanation:
J parses from right to left.
av returns a list of the ascii indexes of each of the characters in its argument, so for example av'ABC' returns 65 66 67.
Then we subtract 64 from each element of that list with the verb 64-~.
Then we convert the list to base 26 using the #. verb.
Brainf*ck, 81 characters (no whitespace)
,[>>>[->>+++++[-<+++++>]<+<]>[-<+>]<<++++++++[<++++++++>-]<[<->-]<[>>>+<<<-],]>>>
Explanation
,[ // get character input into p[0], enter loop if it isn't null (0)
>>>[->>+++++[-<+++++>]<+<] // take what's in p[3] and multiply by 26, storing it in p[4]
>[-<+>] // copy p[4] back to p[3]
<<++++++++[<++++++++>-]< // store 64 in p[1]
[<->-]< // subtract p[1], which is 64, from the input char to get it's alphabetical index
[>>>+<<<-] // add p[0] to p[3]
,] // get another character and repeat
>>> // move to p[3], where our final result is stored
So you'll notice I didn't actually convert the numerical value to an ascii string for printing. That would likely ruin the fun. But I did the favor of moving the pointer to the cell with the result, so at least it's useful to the machine.
Hey, what do you know, I beat C#!
Ruby 1.8.7, 53 50 46 44 24 17 characters
p ('A'..$_).count
Usage:
$ echo -n ROFL | ruby -n a.rb
326676
$ echo -n WTF | ruby -n a.rb
16074
$ echo -n A | ruby -n a.rb
1
APL
13 characters
Put the value in x:
x←'WTF'
then compute it with:
26⊥(⎕aV⍳x)-65
The only reason J beat me is because of the parentheses. I'm thinking there should be some way to rearrange it to avoid the need for them, but it's been a long day. Ideas?
(Heh, you perl programmers with your 30+ character solutions are so cute!)
Excel (not cheating), 25 chars
Supports up to XFD:
=COLUMN(INDIRECT(A1&"1"))
Installation:
Put the formula in cell A2.
Usage:
Enter the column string in cell A1.
Read the result at cell A2.
54 chars, plus a lot of instructions
Supports ROFL also:
(A2) =MAX(B:B)
(B2) =IFERROR(26*B1+CODE(MID(A$1,ROW()-1,1))-64,0)
Installation:
Clear the whole spreadsheet.
Put the formula (A2) in cell A2.
Put the formula (B2) in cell B2.
Fill formula (B2) to as far down as possible.
Usage:
Enter the column string in cell A1.
Read the result at cell A2.
C# 156 146 118 Chars
using System.Linq;class P{static void Main(string[]a){System.Console.Write(
a[0].Aggregate(0,(t,c)=>(t+c-64)*26)/26);}}
Ungolfed:
using System.Linq;
class P
{
static void Main(string[] a)
{
System.Console.Write(a[0]
.Aggregate(0, (t, c) => (t + c - 64) * 26) / 26);
}
}
Golfscript - 16 chars
[0]\+{31&\26*+}*
$ echo -n WTF | ./golfscript.rb excel.gs
16074
$ echo -n ROFL | ./golfscript.rb excel.gs
326676
Haskell, 50 51 56 chars
main=interact$show.foldl(\x->(26*x-64+).fromEnum)0
Usage:
~:166$ echo -n "ROFL" | ./a.out
326676
~:167$ echo -n "WTF" | ./a.out
16074
Python, 64 49 characters
s=0
for c in raw_input():s=26*s+ord(c)-64
print s
You can also replace raw_input() with input() to reduce the character count by 4, but that then requires the input to contain quotation marks around it.
And here's a subroutine that clocks in at 47 characters:
f=lambda x:len(x)and 26*f(x[:-1])+ord(x[-1])-64
k4 (kdb+), 11 characters
26/:1+.Q.A?
Explanation:
k4 parses left of right
.Q.A is defined within k4 - it is the vector "ABC...XYZ"
? is the find operator - the index of the first match for items in the y arg within the x arg
+1 to offset the index
26/: to convert to base 26
One caveat - this will only work where listed types are passed in:
26/:1+.Q.A? "AD"
30
26/:1+.Q.A? "WTF"
16074
but:
26/:1+.Q.A? ,"A"
1
Powershell, 42 chars
[char[]]$args[($s=0)]|%{$s=$s*26+$_-64};$s
JavaScript 1.8: 66 characters
function a(p)Array.reduce(p,function(t,d)t*26+d.charCodeAt()-64,0)
Javascript 1.8: 72 characters
function a(p)(t=0,p.replace(/./g,function(d)t=t*26+d.charCodeAt()-64),t)
JavaScript 1.6: 83 characters
function a(p){t=0;p.split("").map(function(d){t=t*26+d.charCodeAt(0)-64});return t}
JavaScript: 95 characters
function a(p){r=0;t=1;l=p.length;for(i=0;i<l;i++){r+=(p.charCodeAt(l-1-i)-64)*t;t*=26}return r}
JavaScript: 105 characters
function a(p,i){i=i||0;l=p.length;return p?(p.charCodeAt(l-1)-64)*Math.pow(26,i)+a(p.slice(0,l-1),i+1):0}
Usage:
a("A") // 1
a("B") // 2
a("AD") // 30
a("ABC") // 731
a("WTF") // 16074
a("ROFL") // 326676
Scala, 30 chars
print((0/:args(0))(_*26+_-64))"
Example:
C:\>scala -e "print((0/:args(0))(_*26+_-64))" AD
30
C89, 58 characters
s;main(c){while(c=getchar()+1)s=26*s+c-65;printf("%d",s);}
The input (stdin) must contain only A-Z, no other characters (including newlines) are allowed.
Explanation of Concepts - Excelcification
Nice. I wrote my own version of this with a little more explanation a long time ago at
http://aboutdev.wordpress.com/2009/12/19/excelcification-brain-teaser-code/. Although it's not quite an optimized version!
FYI. The base 26 arithmetic is called hexavigesimal and Excel's maximum column is XFD which converts to 16383 (using 0 as the first cell) which is coincidentally exactly 2^14 cells.
Can anyone guess as to why it is 2^14??
Common Lisp, 103 128 characters
(defun x(s)(reduce(lambda(x y)(+(* 26 x)y))(map 'vector(lambda(b)(-(char-code b)(char-code #\A)-1))s)))
C#, 117 111 chars
No contest compared to the likes of Perl, Ruby and APL but an improvement on the other C#/Java answers given so far.
This uses Horner's rule.
class C{static void Main(string[]a){int t=0;foreach(var c in a[0]){t=(t+c-64)*26;}System.Console.Write(t/26);}}
Perl, 34 characters
map$\=26*$\-64+ord,pop=~/./g;print
Thanks to mobrule for several suggestions.
C#, 148 chars
using System;class P{static void Main(string[]a){var r=0d;int j=0,i=a[0].
Length;while(i-->0)r+=(a[0][i]-64)*Math.Pow(26,j++);Console.WriteLine(r);}}
Ungolfed:
using System;
class P
{
static void Main(string[] a)
{
var r = 0d;
int j = 0, i = a[0].Length;
while (i-- > 0)
r += (a[0][i] - 64) * Math.Pow(26, j++);
Console.WriteLine(r);
}
}
Python - 63 chars
>>> f=lambda z: reduce(lambda x,y: 26*x+y, [ord(c)-64 for c in z])
>>> f('ROFL')
326676
Clojure:
user> (reduce #(+ (* 26 %1) %2) (map #(- (int %) 64) "AD"))
30
user> (reduce #(+ (* 26 %1) %2) (map #(- (int %) 64) "ROFL"))
326676
51 characters, plus the number of characters in the input string.
C:
int r=0;
while(*c)r=r*26+*c++-64;
String is stored in 'c', value is in 'r'.
Ruby 1.9, 21 characters
p'A'.upto(gets).count
Tests:
$ echo -n A| ruby x.rb
1
$ echo -n WTF| ruby x.rb
16074
$ echo -n ROFL| ruby x.rb
326676
Common Lisp, 86 characters.
(defun z(s)(let((a 0))(map nil(lambda(v)(setf a(+(* 26 a)(digit-char-p v 36)-9)))s)a))
Java: 112 124 characters
class C{public static void main(String[]a){int r=0;for(int b:a[0].getBytes())r=26*r+b-64;System.out.print(r);}}
Common Lisp, 81 characters
(defun y(s)(reduce(lambda(x y)(+(* 26 x)(-(char-code y)64)))s :initial-value 0))
Funny that as a new user I can post my own answer but not comment on someone else's. Oh well, apologies if I'm doing this wrong!
MATLAB: 24 characters
polyval(input('')-64,26)
Usage:
>> polyval(input('')-64,26)
(after pressing enter) 'WTF'
ans =
16074
Note: You can get it down to 16 characters if you pre-store the string in x, but I kind of thought it was cheating:
>> x = 'WTF'
x =
WTF
>> polyval(x-64,26)
ans =
16074
PHP - 73 Chars
$n=$argv[1];$s=$i=0;while($i<strlen($n))$s=$s*26+ord($n[$i++])-64;echo$s;
Usage:
php -r '$n=$argv[1];$s=$i=0;while($i<strlen($n))$s=$s*26+ord($n[$i++])-64;echo$s;' AA
> 27

Resources