How to remove single quotes around the string

조회 수: 740(최근 30일)
Mekala balaji
Mekala balaji 2017년 8월 14일
편집: Stephen23 2020년 5월 14일
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.
  댓글 수: 3
Walter Roberson
Walter Roberson 2018년 2월 28일
>> T = strsplit('name1,name2,name3,name10',',');
>> fprintf('%s\n', T{:})

댓글을 달려면 로그인하십시오.

채택된 답변

dbmn 2017년 8월 14일
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})
  댓글 수: 3
Jan 2017년 8월 14일
Sorry, dbmn, I did not see your answer, when I typed mine. This is strange, because yours is 2 hours older.

댓글을 달려면 로그인하십시오.

추가 답변(3개)

Jan 2017년 8월 14일
편집: Jan 2017년 8월 14일
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?
  댓글 수: 5
Stephen23 2020년 5월 14일
편집: Stephen23 2020년 5월 14일
"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!

댓글을 달려면 로그인하십시오.

Jim Hokanson
Jim Hokanson 2020년 5월 13일
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

Peng He
Peng He 2019년 1월 23일
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 1월 23일
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')

댓글을 달려면 로그인하십시오.


Find more on Data Import from MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by