Why can't I access characters in strings in Matlab? - matlab

Not sure what I am doing wrong here. I've seen online that characters in a string can be accessed using normal indexing (e.g a_string_variable(1:5)) however when I do this I get a 1x0 empty string array or even worst an error.
I've attached a screen shot of my Matlab command window below to show you what I'm doing and the error I am getting.

A string formed with the double quotes " is treated as if the entire string is one element of the variable. Kind of like a cell array. This is a type of classdef OOP object.
A string formed with the single quotes ' is treated as an array of characters, with each character being one element of the variable.
To get the behavior you are apparently wanting, use the single ' quotes.

Related

Postgres storing and retrieving double quotes

I am storing data which includes single and double quotes in a column which has a type of text.
I have a function that returns that column but it always returns the double quote character being doubled up. I tried to escape the character \" but that just returned \"". I have the same process working in MySQL well. I am trying to move to Postgres for greater scaling and stability but it seems it does not work well. I have noticed in pgAdmin when I look at the data output, it seems to show the data correctly so I am wondering what is being done there for it to work correctly?
Also I am getting a (" at the start of the returned value and ") at the end.
Can someone help please?
Use pg_escape_string to escape quote characters in your string.

crystal reports attempting to link two tables by matching string with no luck

As stated in the title, I have two tables I'm attempting to link. Both Strings appear to be a match, however Crystal Reports is not picking it up. The only thing I can think is that that length of the field is different, even though the strings are the same. could that cause a discrepancy? If so how can I correct for it? Thank you
Length of the string will prevent a match. If you are using the Trim(string) function, that only removes spaces found at the beginning or end of your string, so the two strings could still be of different lengths after using this function. You will need to use another function to capture a substring of the original string. To do this you can use the Left(string, length) function to ensure both strings are the same length.
If they still do not match then you may have non-printable characters in one or both of your strings. Carriage Return and Line Feed tend to be the most commonly found non-printable characters. A Carriage Return is represented as Chr(10), while a Line Feed is represented as Chr(13). These are Built In Constants similar to those found in VBA and Visual Basic.
You can use a find and replace to remove them with the following formula. Its not a bad idea to also include the trim and left functions in this as well to ensure you get the best match possible.
Replace(Replace(Left(Trim({YourStringField}), 10),Chr(10), ""),Chr(13), "")
There are a few additional Built In Constants you may need to check for if this doesn't work. A Tab is represented as Chr(9) for example. Its very rare for strings to contain the other Built In Constants though. In most cases Carriage Return and Line Feed are the only ones that are typically found in Plain Text. Tabs and the other constants should only be found in Rich Text and are very rare in string data.

Inputing '\' before a character in a string Matlab

I'm trying to insert a '\' into a string array before each '_' in the string. The reason I am trying to do this is to keep the format of the sheetnames I pull from an xls file when i use the sheetnames in the legend of a plot. I tried using:
legend(sheetname{n},'Interpreter','none','Location','East')
but it just adds the two commands as two other labels in the legend. Hense this solution. I have looked into searching through sheetnames and have found how to replace, but I don't want to replace the characters if possible. If there is a way to do this please let me know.
Try this instead:
legend(sheetname(n),'Interpreter','none','Location','East');
Note the use of parentheses instead of curly braces, which passes a cell to LEGEND instead of the contents of the cell (i.e. cell indexing instead of content indexing). This should allow the additional parameter/value pairs to be properly interpreted.
If you want to apply a string replacement function on each cell element, you can do this as follows:
cellfun(#(x) strrep(x,'_','\_'),sheetname,'uniformoutput',false)
this applies strrep(x,'_','\_') to all elements of sheetnames and outputs it as a new cell array (because of 'uniformoutput',false).

Determine if non-numerical characters have been pasted into UITextField

For a specialized calculator I would like to allow copy / paste for a textfield which is meant for numerical values only. So, only numerical characters should be actually pasted or the pasted string should be rejected if it contains non-numerical characters.
I was thinking about using UITextFieldDelegates textField:shouldChangeCharactersInRange:replacementString: method to check the pasted string for non-numerical characters. But NSString offers no method for checking whether it does NOT contain characters specified in a single set. So this way I would need to check occurances of characters from several sets, which is clumsy and these checks would run for every single number that would be typed in, which appears like quite some overhead to me.
Another way would be to iterate and check for every character in the replacement string whether there's a match in a numerical set.
Either way would propably work, but I feel like I'm missing something.
Do you have any advice? Is there a convenience method to achieve this?
But NSString offers no method for checking whether it does NOT contain characters specified in a single set
sure it does.
if([myString rangeOfCharacterFromSet:myCharacterSet].location ==NSNotFound)
{
//means there is no character from specified set in specified string
}

String to Unicode in C#

I want to use Unicode in my code. My Unicode value is 0100 and I am adding my Unicode string \u with my value. When I use string myVal="\u0100", it's working, but when I use like below, it's not working. The value is looking like "\\u1000";. How do I resolve this?
I want to use it like the below one, because the Unicode value may vary sometimes.
string uStr=#"\u" + "0100";
There are a couple of problems here. One is that #"\u" is actually the literal string "\u" (can also be represented as "\u").
The other issue is that you cannot construct a string in the way you describe because "\u" is not a valid string by itself. The compiler is expecting a value to follow "\u" (like "\u0100") to determine what the encoded value is supposed to be.
You need to keep in mind that strings in .NET are immutable, which means that when you look at what is going on behind the scenes with your concatenated example (`#"\u"+"0100"), this is what is actually happening:
Create the string "\u"
Create the string "0100"
Create the string "\u0100"
So you have three strings in memory. In order for that to happen all of the strings must be valid.
The first option that comes to mind for handling those values is to actually parse them as integers, and then convert them to characters. Something like:
var unicodeValue = (char)int.Parse("0100",System.Globalization.NumberStyles.AllowHexSpecifier);
Which will give you the single Unicode character value. From there, you can add it to a string, or convert it to a string using ToString().