Reading a CSV file with XLSread, convert raw data to strings

How do I convert this cell array to a char array (obtained by reading in from a csv file using xlsread and the "raw" argument
A =
[ 1]
[ 2]
[ 3]
'Y'
[ 5]
[ 6]
[ 7]
'Y'
'X'
[ 9]
[10]
I need to output this to a text file and have used:
for row = 1:numel(A)
fprintf(fileID,'%s\n',A{row});
end
but I get strange characters:

 採用された回答

Stephen23
Stephen23 2015 年 4 月 13 日
編集済み: Stephen23 2015 年 4 月 13 日

0 投票

Given a cell array of mixed numeric and string data:
>> A = {1;2;3;'Y';5;6;7;'Y';'X';9;10};
convert to a cell array of strings using cellfun and num2str (which ignores the strings):
>> B = cellfun(@num2str,A, 'UniformOutput',false)
B =
'1'
'2'
'3'
'Y'
'5'
'6'
'7'
'Y'
'X'
'9'
'10'
And convert a cell array of strings to a pure character array by wrapping this in char:
>> char(B)
ans =
1
2
3
Y
5
6
7
Y
X
9
10

3 件のコメント

Jason
Jason 2015 年 4 月 13 日
Just so I understand, this line:
cellfun(@num2str,A, 'UniformOutput',false)
will just convert the numbers to strings, leaving the strings alone? Thanks
Guillaume
Guillaume 2015 年 4 月 13 日
編集済み: Guillaume 2015 年 4 月 13 日
cellfun calls the function you give it (in this case num2str) passing it in turn each element of the input cell array(s). It collects the output(s) of the function in a cell array the same shape as the input, if you pass it 'UniformOutput', false (or an plain array otherwise).
It is equivalent to writing a for loop over each element of the cell array and calling num2str within the loop, except you don't have to predeclare the output, nor do you have to worry about finding the bounds of the loop.
In this case, the line is equivalent to:
B = cell(size(A));
for idx = 1:numel(A)
B{idx} = num2str(A{idx});
end
Jason
Jason 2015 年 4 月 14 日
Thankyou

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

質問済み:

2015 年 4 月 13 日

コメント済み:

2015 年 4 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by