String indexing in MATLAB: single vs. double quote - string

I have a matrix of strings such as the following:
readFiles = [
"11221", "09";
"11222", "13";
"12821", "06";
"13521", "02";
"13522", "13";
"13711", "05";
"13921", "01";
"14521", ".001";
"15712", ".003"
];
These are used to access to some folders and files in an automatic way. Then what I want to do is the following (with ii being some integer):
FileName = strcat('../../Datasets/hc-1/d',readFiles(ii,1),'/d',...
readFiles(ii,1),readFiles(ii,2),'.dat');
data(ii,:) = LoadBinary(FileName, 6);
The string FileName is then generated using double quotes (I'm not sure why). So its value is:
FileName =
"../../Datasets/hc-1/d13921/d1392101.dat"
The function LoadBinary() returns an error when trying to perform the following operation:
lastdot = strfind(FileName,'.');
FileBase = FileName(1:lastdot(end)-1); % This line
However, if I create the string FileName manually using single quotes, the function works okay.
In a nutshell, if I try to index a string (FileName(1:lastdot(end)-1)) that is created with the lines above (leading to FileName = "../../Datasets/hc-1/d13921/d1392101.dat"), MATLAB returns an error. If I create it manually with single quotes (FileName = '../../Datasets/hc-1/d13921/d1392101.dat'), the function works right.
Why does this happen? Is there a way to fix it (i.e. convert the double-quoted string into a single-quoted one)?

Double quotes are String array, while Single one are Char array. You can convert your string array to a char one using the function char.
So you'd write :
CharFileName = char(FileName)
And it should resolve your issue.

Related

Error using dir Function is not defined for 'string' inputs

I'm writing my first Matlab script, and I get an error trying to use dir(). This is the script:
strLocation = "C:\Users\myname\Documents\MATLAB";
listing = dir(strLocation)
The error is:
Error using dir
Function is not defined for 'string' inputs.
What am I doing wrong?
It should first be noted that a char vector and a string are different things in Matlab. The string data type was introduced recently (in R2016b, I think). Previous versions do not support the string type, only char vectors.
Since the string data type was introduced, many built-in functions that used to accept char vector input can now accept string input as well. But this is being gradually incorporated into functions, apparently. So, even if your Matlab version supports the string data type, you may find some functions that still can only take a char vector as input. This seems to the case for dir in your version. In R2018b dir supports both types of input, according to the documentation.
So, you need to define the input to dir as a char vector. For this you use ' instead of ":
strLocation = 'C:\Users\myname\Documents\MATLAB';
listing = dir(strLocation)
Or, if you must have a string, convert it to a char vector before passing it to dir:
strLocation = "C:\Users\myname\Documents\MATLAB";
listing = dir(char(strLocation))
Since MATLAB R2017a double quotation marks denotes strings and single quotation marks denotes character vectors.
The dir function requires a char vector so you should call it with single quotation marks,
strLocation = 'C:\Users\myname\Documents\MATLAB';
listing = dir(strLocation)

how to separate a string char by char and add a symbol after in c#

Any idea how i can separate a string with character and numbers, for example
12345ABC678 to make it look like this
1|2|3|4|5|A|B|C|6|7|8??
Or if this is not possile, how can i take this string a put every character or nr of it in a different textBox like this?
You can use String.Join and String.ToCharArray:
string input = "12345ABC678";
string result = String.Join("|", input.ToCharArray());
Instead of ToCharArray(creates a new array) you could also cast the string to IEnumerable<char> to force it to use the right overload of String.Join:
string result = String.Join("|", (IEnumerable<char>)input);
use
String aString = "AaBbCcDd";
var chars = aString.ToCharArray();
Then you can loop over the array (chars)

Add 'r' prefix to a python variable

I have string variable which is
temp = '1\2\3\4'
I would like to add a prefix 'r' to the string variable and get
r'1\2\3\4'
so that I can split the string based on '\'. I tried the following:
r'temp'
'r' + temp
r + temp
But none of the above works. Is there a simple to do it? I'm using python 3. I also tried to encode the string, using
temp.encode('string-escape')
But it returns the following error
LookupError: unknown encoding: string-escape
r is a prefix for string literals. This means, r"1\2\3\4" will not interpret \ as an escape when creating the string value, but keep \ as an actual character in the string. Thus, r"1\2\3\4" will have seven characters.
You already have the string value: there is nothing to interpret. You cannot have the r prefix affect a variable, only a literal.
Your temp = "1\2\3\4" will interpret backslashes as escapes, create the string '1\x02\x03\x04' (a four-character string), then assign this string to the variable temp. There is no way to retroactively reinterpret the original literal.
EDIT: In view of the more recent comments, you do not seem to, in fact, have a string "1\2\3\4". If you have a valid path, you can split it using
path.split(r'\')
or
path.split('\\')
but you probably also don't need that; rather, you may want to split a path into directory and file name, which is best done by os.path functions.
Wouldn't it just be re.escape(temp)?
Take for example the use case of trying to generate a pattern on the fly involving word boundaries. Then you can do this
r'\b' + re.escape(temp) + r'\b'
just to prefix r in variable in search, Please do this r+""+temp.
e.g.-
import re
email_address = 'Please contact us at: support#datacamp.com'
searchString = "([\w\.-]+)#([\w\.-]+)"
re.serach(r""+searchString, email_address)

Getting substrings from a string in c# in the format Domain\Alias

I have a variable which has strings stored in the format "domain\alias" and I want to split this in two different strings domain and alias.
I have two solutions for the above case, but none of them are working in my case.
solution 1: separating alias from the string.
for this I am using the code below:
int index = name.IndexOf("\") + 1;
string piece = name.Substring(index);
where name is the variable which stores the string in the format "domain\alias"
This solution doesn't work for '\' however it works in case of '.'
solution 2:
separating domain from the string.
Here I got a solution below:
var domainFormattedString = #"fareast\v-sidmis";
var parts = domainFormattedString.Split('\\');
var domainString = parts[0];
return domainString;
this works, but it needs a string prefixed with #symbol and i have my string stored in the variable name for which this solution doesn't work.
Someone please help me to extract the two substrings from my variable name.
EDIT 1: Thanks all for your help! I figured out the issue...when i explicitly declare a string as: var x = "domian\alias" it creates and issue as \ is treated as a escape character by c# so i had to append # at the beginning. But I got to know that when a string is read from a user, the solution works!
\ has a special meaning so you need to override the escape sequence to be treated as normal character with another escape character.
string input = #"domain\alias";
int inputindex= input.IndexOf("\\");
string domain = input.Substring(0, inputindex);
string alias = input.Substring(inputindex+1);
Hope It helps eventhough better late than never :)

groovy replace double quotes with single and single with double

I have a string "['type':'MultiPolygon', 'coordinates':[[73.31, 37.46], [74.92, 37.24]]]"
How can I replace all single quotes with double quotes and double to with single?
The result should be like this:
'["type":"MultiPolygon", "coordinates":[[73.31, 37.46], [74.92, 37.24]]]'
From link given by #yate, you can find a method:
tr(String sourceSet, String replacementSet)
and apply that to your string as:
def yourString = ...
def changedString = yourString.tr(/"'/,/'"/)
that will do the job.
You want to use the replaceAll method. Since the first conversion will be overridden by the second, you may need a temporary variable:
String replacePlaceholder = '%%%' // Some unlikely-to-occur value
yourString = yourString.replaceAll('\'', replacePlaceholder)
.replaceAll('"', '\'')
.replaceAll(replacePlaceholder, '"')
It's certainly not the most efficient way to do it, but it's a start.

Resources