MATLAB Answers

How to remove single quotes around the string

920 views (last 30 days)
Mekala balaji
Mekala balaji on 14 Aug 2017
Edited: Stephen Cobeldick on 14 May 2020
I have my string (which is actually a header line read from CSV file). single string in a row separated by somas as below:
I tried with both "strsplit and regexp" to split the string at "comma" and save to another variable as a column, but it giving me single quotes around each substring as follows:
my desired output:
Please someone help, many thanks in advance.


Stephen Cobeldick
Stephen Cobeldick on 14 Aug 2017
"...but it giving me single quotes around each substring as follows..."
How values (numeric, string, char, etc) are displayed is a totally different thing to they are stored. Do not confuse the two! There does not appear to be any problem at all.
Harold Rocha
Harold Rocha on 28 Feb 2018
There is a typographical problem if you want to copy the output into something. I would be happy to find out how to remove the apostrophes/single quotes as well.
Walter Roberson
Walter Roberson on 28 Feb 2018
>> T = strsplit('name1,name2,name3,name10',',');
>> fprintf('%s\n', T{:})

Sign in to comment.

Accepted Answer

dbmn on 14 Aug 2017
In Matlab, Strings (and Substrings) begin and end with the ' character. See Matlab Documentation for Char ans Strings
This character is not embedded in the string itself, but helps you figure out that you deal with a variable of datatyp char
  • If you just want to work with them - don't worry, the ' will not be "baked" into your data
  • if you want to output that data, you could use something like:
fprintf('%s\n', input{:})
  • Oh and if you want to export to excel, dont worry, Matlab takes care of your '. The following code produces a file without the '
xlswrite('myfile', {'asd', 1, 2; 'qwe', 3, 4})


Mekala balaji
Mekala balaji on 14 Aug 2017
I don't want to print, in fact, I want to store them in to a variable. Is it possible to store into a variable using fprintf('%s\n', input{:})
Stephen Cobeldick
Stephen Cobeldick on 14 Aug 2017
"Is it possible to store into a variable using fprintf('%s\n', input{:})"
Not really. If you read the fprintf documentation it states: "Write data to text file": how do you imagine that writing to file has anything to do with assigning a value to a variable? fprintf could print something to the command window, but that does not create a variable either.
Assining values to variables is covered very well in the MATLAB Introductory Tutorials, which are highly recommended for all beginners:
You should also read this:
Jan on 14 Aug 2017
Sorry, dbmn, I did not see your answer, when I typed mine. This is strange, because yours is 2 hours older.

Sign in to comment.

More Answers (3)

Jan on 14 Aug 2017
Edited: Jan on 14 Aug 2017
The quotes do not belong to the data. They are just added, when you display the cell string in the command window. Try this:
str = 'name1,name2';
cstr = strsplit(str, ',')
This is shown in the command window:
cstr =
1×2 cell array
'name1' 'name2'
But the strings, the elements of the cell string, do not contain the quotes:
fprintf('%s\n', cstr{1});
any(cstr{1} == char(39)) % 0: No, the string does not contain a quote character
Please contain, what exactly "desired output" means: Do you mean the output to the command window or the contents of the variable?


Show 2 older comments
Joseph Transom
Joseph Transom on 29 Jul 2019
This is somewhat bemusing.
Consider if a piece of text that represents a range, is imported from an external source and arrives as a cell - e.g.,
pRng = tableList{l,4}; % pRing will be class 'cell'
let's assume that the person who crafted the Excel file was very careful with the relevant column: the things in that column look like this -
:,[1 2 4:end]
No quotes; no parentheses - no extravangances whatsoever, because this is going into MATLAB, which means you have to tell it things LI5YO.
Let's examine it after import -
>> pRng
pRng =
1×1 cell array
{':, [1 2 4:end]'}
>> class(pRng)
ans =
>> pRng{1}
ans =
':, [1 2 4:end]'
>> class(pRng{1})
ans =
OK, that seems pretty string-y.
In a well-structured programming language, pRng would be useful for a bunch of things, including acting as an array index. That would even work in VBA (assuming that instead of MATLAB array-indices, it was Excel range references).
Now let's use it somewhere...
T(pRng{1}); % BZZZZT!!!
Index exceeds the number of array elements (80).
If pRng{1} is an unescaped string, the line above should be equivalent to
T(:, [1 2 4:end]);
Seems legit, no?
If you thought that, you would be wrong (in MATLAB).
Just to be sure it's not something else... let's check T -
>> size(T)
ans =
5 16
>> class(T)
ans =
T(:,[1 2 4:end])
is a perfectly valid expression, and yields what is expected (i.e., T excluding column 3).
On the other hand entirely...
does not evaluate to what is expected if pStr is an unescaped string (i.e., a sequence of char elements that should be indistinguishable from what you get from keystrokes).
The way to get that "string" to work?
Split it up, turn the numbers into numbers, and use the numbers as inputs into the array index.
So in a quite-important context, pStr{1} is NOT - canonically, operationally, literally NOT - equal to :,[1 2 4:end] ... alternatively, MATLAB's array-referencing mechanism isn't capable of parsing it. I don't know which would be worse.
As an aside: it's not like this example is rocket surgery: it's a good example of the type of kludge that happens when it's necessary to do a quick, one-off, set of tasks. I do precisely the same sort of shenanigans all the time in other systems: R, Python, plpgsql, JavaScript... even VBA.
MATLAB's undocumented "anti-features" - e.g., being unable to feed a supposed 'raw' string into a supposed array-reference parser - is why R and Pyton/numpy/pandas are so dominant now.
Walter Roberson
Walter Roberson on 29 Jul 2019
MATLAB treats character vectors as vectors of values when used as indices, with each character being treated as its underlying utf16 ordinate position.
LC = 1:255;
LC('A' :'Z') = 'a':'z' ;
S = 'Hi There!'
Will produce 'hi there' by way of array indexing.
If you want to do the kind of kludge that mention then matlab provides eval()
I doubt that you will find any programming languages (except awk and derivatives) in which indexing with '1' would be treated the same as if you had indexed with 1
Stephen Cobeldick
Stephen Cobeldick on 14 May 2020
"I do precisely the same sort of shenanigans all the time in other systems: R, Python, plpgsql, JavaScript... even VBA. "
Using a string as a list index with IPython throws an error:
L = [1,2,3,4]
Traceback (most recent call last):
File "<ipython-input-2-69e97d48164a>", line 1, in <module>
TypeError: list indices must be integers or slices, not str
Does not work in R either:
x <- c(1,2,3)
[1] NA
R does allow the elements to be named, but then those are arbitrary names which do not correspond to indices.
Apparently JavaScript and VBA do permit using strings of numbers as indices, VBA even silently truncates any fractional part... the horror!

Sign in to comment.

Jim Hokanson
Jim Hokanson on 13 May 2020
Since I stumbled across this when looking at ways of copying variables, specifically cell arrays of strings, from Matlab into Excel without single quotes I'll provide that solution here. With referencing a CSV file my guess is that the author was asking that question as well, although perhaps not as clearly ...
Converting a cell array of strings to a string array will all you to copy into Excel without the quotes.
A = {'name1'
B = string(A);
open B
%Right click in the variable editor and copy, then paste into Excel


Sign in to comment.

Peng He
Peng He on 23 Jan 2019

  1 Comment

Walter Roberson
Walter Roberson on 23 Jan 2019
If you were going to do that you might as well just do
YourArray = 'name1,name2,name3,name10';
YourArray(YourArray == ',') = char(10);
YourArray = strrep(YourArray, ',', char(10))
YourArray = regexprep(YourArray, ',', '\n')

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!