Textscan reading file and surrounding text with single quotes

I've realized that, in the simple function I'm trying to write, textscan appears to be reading in the text and surrounding it with single quotes. Here's what I mean:
Test file contents: ABCCBDXYZABCZZ
MATLAB commands:
fileID=fopen(FILENAME); str=textscan(fileID,'%s');
The result of this is a 1x1 cell, as expected, however the contents of the cell are: 'ABCCBDXYZABCZZ' instead of ABCCBDXYZABCZZ
This is proving to be an issue because accessing the cell contennts with "string = str{1}" only returns a cell, instead of an actual string- so I can't do any string operations on it. Is this intended behavior?
When I manually create a cell with the following:
cell_test = {'ABCDEF'};
the workspace shows the variable is a 1x1 cell, but the contents of the cell are ABCDEF without the single quotes, so if I do
string_test = cell_test{1}
it sets the actual string to that variable, instead of the string surrounded by single quotes, which is treated as another cell.
I can't find this issue anywhere else- it's possible that I've looked in the wrong place, but all the results are about not accessing the cell contents properly, which I don't believe I'm suffering from.

 Accepted Answer

>> fileContent = 'ABCD 8 10.2' ; % Fake content for the ex.
>> columns = textscan(fileContent, '%s %d %f') ;
>> class(columns)
ans =
cell
>> size(columns)
ans =
1 3
TEXTSCAN outputs a cell array of columns, as shown above (each cell of its output contain a column of data). When the data type/class of a column allows it, the column is stored as a numeric array; it is stored as a cell array otherwise (e.g. for "strings").
Accessing the content of cell 1 of cell array columns (using {}), we get..
>> class(columns{1})
ans =
cell
So cell 1 of columns is a cell array in itself. Cell 1 of this cell array contains the string 'ABCD' (where single quotes are the delimiter for strings). Note that displaying a string (e.g. with DISP which is implicit) in the command window doesn't display delimiters
>> class(columns{1}{1}) % Element 1 of column 1.
ans =
char
>> columns{1}{1}
ans =
ABCD
Columns 2 and 3 are numeric, so the content of cells 2 and 3 of columns are not cell arrays but numeric arrays:
>> class(columns{3}) % Numeric array of doubles.
ans =
double
>> class(columns{3}(1)) % Element 1 is a double.
ans =
double
>> columns{3}(1)
ans =
10.2000

3 Comments

Thank you very much. I thought I had read about all the ways to address cell arrays but obviously I had not. Thanks again!
You're welcome! The most crucial thing to remember is that block indexing (using parentheses) returns cells, and indexing a cell with curly brackets returns its content.
>> ca = {'Freq', 8} ;
>> class( ca )
ans
cell
>> class( ca(1) ) % Cell 1 of ca.
ans
cell
>> class( ca{1} ) % Content of cell 1 of ca.
ans
char
Finally, addressing multiple cells' content returns a comma separated list (CSL), which is useful for "vectorizing" arguments in function calls, e.g.
foo( ca{:} ) ;
is equivalent to
foo( ca{1}, ca{2} ) ;
so you see that CSL are a way to "distribute" cells separated by commas the way you list arguments in function calls.
Can the single quotes just be striped off, and the new value replaced into the same array?

Sign in to comment.

More Answers (1)

Categories

Asked:

on 1 Sep 2013

Commented:

on 24 Mar 2016

Community Treasure Hunt

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

Start Hunting!