Reading a string with %

3 次查看(过去 30 天)
Charlotte
Charlotte 2014-10-13
评论: Charlotte 2014-10-13
Hello,
I am trying to read in a list of strings from a .xls file, some of which contain a %. Since this is generally used to add comments to a block of code, the stings that contain %'s are being read into MATLAB as ''. Is there any way to read these strings into MATLAB so that they include the %? For example
'hello%mynameis'
, or should I just scrap them from my list?
Many Thanks, Charlie
  1 个评论
Jan
Jan 2014-10-13
编辑:Jan 2014-10-13
Please post your current code. Why do you assume that the % characters are not imported? Did you check this by something like this:
s = 'hello%mynameis'
fprintf(s)
Then try:
fprintf('%s', s)

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2014-10-13
Could you show the portion of code you're using to read your excel file?
The first output of xlsread is only the numeric content of the Excel file. However, you should be able to extract your string from the second or third output. That is, if all you're doing is:
data = xlsread('somefile.xlsx');
now do:
[data, text, raw] = xlsread('somefile.xlsx');
  2 个评论
Charlotte
Charlotte 2014-10-13
I have been using the second method you suggested. Here is my code when I read the data into MATLAB:
[num2, txt2, raw2] = xlsread('MasterList.xlsx', 'Sheet1', 'C2:C50'); wikiNames = txt2;
Is this correct? Also what about other strings that contain other weird symbols like Æterna_Zentaris? Will this method work for these as well?
Charlotte
Charlotte 2014-10-13
I just implemented this method in MATLAB and it worked great for all symbols. Thank you!

请先登录,再进行评论。

更多回答(1 个)

dpb
dpb 2014-10-13
You don't give any other information on what you tried, but there's no reason you can't read such strings into Matlab...
>> l='hello%mynameis';
>> textscan(l,'%s')
ans =
{1x1 cell}
>> ans{1}
ans =
'hello%mynameis'
>>
What specific file format did you have and what specific problem did you have with reading it with what specific statement/function?
  1 个评论
Charlotte
Charlotte 2014-10-13
The file is a .xls file and I have been attempting to read in the data using xlsread since some of the data are dates and some of the data are strings. If I remember correctly, textscan does not work for excel files?

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by