Subset a string - matlab

It's easy to subset a character vector:
>> c = 'hello world';
>> c(1:5)
ans =
'hello'
But that doesn't work for strings:
>> s = "hello world";
>> s(1:5)
Index exceeds the number of array elements (1).
How do I subset a string?

You can use the extractBetween function:
>> s = "hello world";
>> extractBetween(s, 1, 5)
ans =
"hello"

Related

how to read broken numbers on two lines in a text file in matlab?

how to read broken numbers on two lines in matlab?
I am generating some results in text files that are being broken into two lines. Example:
text x = 1.
2345 text
What would a code look like to read the value x = 1.2345?
Suppose the value of x = 1.2345 is in file named name.txt.
When it doesn't break the values I'm looking for:
text x = 1.2345 text
I use the following (working) code:
buffer = fileread('name.txt') ;
search = 'x = ' ;
local = strfind(buffer, search);
xvalue = sscanf(buffer(local(1,1)+numel(search):end), '%f', 1);
You can remove line breaks (and other "white space", if needed) before parsing the string:
>> str = sprintf('text x = 1.\n2345 text')
str =
'text x = 1.
2345 text'
>> str = regexprep(str, '\n', '')
str =
'text x = 1.2345 text'

Calculating Factorials using QBasic

I'm writing a program that calculates the Factorial of 5 numbers and output the results in a Tabular form but I keep getting Zeros.
Factorial Formula:. n! = n×(n-1)!
I tried:
CLS
DIM arr(5) AS INTEGER
FOR x = 1 TO 5
INPUT "Enter Factors: ", n
NEXT x
f = 1
FOR i = 1 TO arr(n)
f = f * i
NEXT i
PRINT
PRINT "The factorial of input numbers are:";
PRINT
FOR x = 1 TO n
PRINT f(x)
NEXT x
END
and I'm expecting:
Numbers Factorrials
5 120
3 6
6 720
8 40320
4 24
You did some mistakes
FOR i = 1 TO arr(n)
where is n defined
you also never stored actual values into arr
PRINT f(x)
here you take from array f that is also not defined in your code
Possible solution to calculate arrays of factorials:
CLS
DIM arr(5) AS INTEGER
DIM ans(5) AS LONG
FOR x = 1 TO 5
INPUT "Enter Factors: ", arr(x)
f& = 1
FOR i = 1 TO arr(x)
f& = f& * i
NEXT i
ans(x) = f&
NEXT x
PRINT
PRINT "The factorial of input numbers are:";
PRINT
PRINT "Numbers", "Factorials"
FOR x = 1 TO 5
PRINT arr(x), ans(x)
NEXT x
END
I don't have a BASIC interpreter right in front of me, but I think this is what you're looking for:
CLS
DIM arr(5) AS INTEGER
DIM ans(5) AS LONG 'You need a separate array to store results in.
FOR x = 1 TO 5
INPUT "Enter Factors: ", arr(x)
NEXT x
FOR x = 1 to 5
f& = 1
FOR i = 1 TO arr(x)
f& = f& * i
NEXT i
ans(x) = f&
NEXT x
PRINT
PRINT "The factorial of input numbers are:";
PRINT
PRINT "Numbers", "Factorials"
FOR x = 1 TO 5
PRINT STR$(arr(x)), ans(x)
NEXT x
END
Just a comment though: In programming, you should avoid reusing variables unless you are short on memory. It can be done right, but it creates many opportunities for hard to find bugs in larger programs.
Possible solution to calculate arrays of factorials and square roots:
CLS
PRINT "Number of values";: INPUT n
DIM arr(n) AS INTEGER
DIM ans(n) AS LONG
FOR x = 1 TO n
PRINT "Enter value"; x;: INPUT arr(x)
f& = 1
FOR i = 1 TO arr(x)
f& = f& * i
NEXT i
ans(x) = f&
NEXT x
PRINT
PRINT "The factorial/square root of input numbers are:";
PRINT
PRINT "Number", "Factorial", "Squareroot"
FOR x = 1 TO n
PRINT arr(x), ans(x), SQR(arr(x))
NEXT x
END

Matlab: Meaning of '\a' or "alarm" in sprintf/fprintf

In the documentations of sprintf and fprintf, in the list of special output format characters/operators, there is the '\a' character and they say it's "Alarm".
I tried printing it and it gives me an empty output.
>> sprintf('\a')
ans =
>>
What does it do?
It's the ASCII BEL (0x07) character:
>> sprintf('\a')+0
ans = 7
>> a = sprintf('\a')
a =
>> b = char(7)
b =
>> a==b
ans = 1
It's normally used to make the console beep, but it doesn't do anything in my Octave command window.

VBScript function to convert hex to unicode

I have hexadecimal string which consists of different languages letters.
Please help me with a vb-script function which converts this hexadecimal string to Unicode text.
For hex string "506F7274756775C3AA73" , I need to get "Português" as output.
I tried following function, it gives "Português" as output.
MsgBox ConvertHexToUnicode("506F7274756775C3AA73")
Function ConvertHexToUnicode(hexString)
Dim Strlen
Dim Charaset_array(20)
Dim i
Dim j
Strlen = Len(hexString)
i = 0
j = 1
Do
Charaset_array(i) = Mid(hexString,j, 2)
i = i + 1
j = j + 2
Loop While j < Strlen
ConvertHexToUnicode = ""
For Each chara In Charaset_array
If Not(IsEmpty(chara)) Then
ConvertHexToUnicode = ConvertHexToUnicode + ChrW("&H" & chara )
End If
Next
End Function
Use Mid() to cut your input string into hex numbers (strings), prepend &H to get hex literals, and ChrW() to build characters:
>> s = "00001F00"
>> WScript.Echo Mid(s, 5, 4)
>> WScript.Echo "&H" & Mid(s, 5, 4), CLng("&H" & Mid(s, 5, 4))
>> WScript.Echo ChrW("&H" & Mid(s, 5, 4)), AscW(ChrW("&H" & Mid(s, 5, 4)))
>>
1F00
&H1F00 7936
ἀ 7936

Prefix match in MATLAB

Hey guys, I have a very simple problem in MATLAB:
I have some strings which are like this:
Pic001
Pic002
Pic003
004
Not every string starts with the prefix "Pic". So how can I cut off the part "pic" that only the numbers at the end shall remain to have an equal format for all my strings?
Greets, poeschlorn
If 'Pic' only ever occurs as a prefix in your strings and nowhere else within the strings then you could use STRREP to remove it like this:
>> x = {'Pic001'; 'Pic002'; 'Pic003'; '004'}
x =
'Pic001'
'Pic002'
'Pic003'
'004'
>> x = strrep(x, 'Pic', '')
x =
'001'
'002'
'003'
'004'
If 'Pic' can occur elsewhere in your strings and you only want to remove it when it occurs as a prefix then use STRNCMP to compare the first three characters of your strings:
>> x = {'Pic001'; 'Pic002'; 'Pic003'; '004'}
x =
'Pic001'
'Pic002'
'Pic003'
'004'
>> for ii = find(strncmp(x, 'Pic', 3))'
x{ii}(1:3) = [];
end
>> x
x =
'001'
'002'
'003'
'004'
strings = {'Pic001'; 'Pic002'; 'Pic003'; '004'};
numbers = regexp(strings, '(PIC)?(\d*)','match');
for cc = 1:length(numbers);
fprintf('%s\n', char(numbers{cc}));
end;