Reading a string with %

6 views (last 30 days)
Charlotte
Charlotte on 13 Oct 2014
Commented: Charlotte on 13 Oct 2014
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 Comment
Jan
Jan on 13 Oct 2014
Edited: Jan on 13 Oct 2014
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)

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 13 Oct 2014
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 Comments
Charlotte
Charlotte on 13 Oct 2014
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 on 13 Oct 2014
I just implemented this method in MATLAB and it worked great for all symbols. Thank you!

Sign in to comment.

More Answers (1)

dpb
dpb on 13 Oct 2014
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 Comment
Charlotte
Charlotte on 13 Oct 2014
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?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!