Got an error when using cell2csv in Matlab 2014b version

조회 수: 5 (최근 30일)
Simon Wang
Simon Wang 2015년 2월 23일
답변: Simon Wang 2015년 2월 27일
The error message is the following
Undefined function 'cell2csv' for input arguments of type 'cell'.
My Matlab code: cell2csv(fileName, aCellInstance);
Can you help to see if I used it in the wrong way or the function is not available in 2014b.
Thanks!
Simon

답변 (5개)

Abhishek GS
Abhishek GS 2015년 2월 24일
Simon,
cell2csv is not a MATLAB built in function. But if you want to write a cell array into a comma separated value text, you could use some of the files from MATLAB Central which can be found here , here , here and here .

Konstantinos Sofos
Konstantinos Sofos 2015년 2월 24일
Hi Simon, As Abhishek mentioned cell2csv is not a MATLAB built in function. You can use something from MATLAB file exchange e.g.
function cell2csv(filename,cellArray,delimiter)
% Writes cell array content into a *.csv file.
%
% CELL2CSV(filename,cellArray,delimiter)
%
% filename = Name of the file to save. [ i.e. 'text.csv' ]
% cellarray = Name of the Cell Array where the data is in
% delimiter = seperating sign, normally:',' (it's default)
%
% by Sylvain Fiedler, KA, 2004
% modified by Rob Kohr, Rutgers, 2005 - changed to english and fixed delimiter
if nargin<3
delimiter = ',';
end
datei = fopen(filename,'w'); for z=1:size(cellArray,1) for s=1:size(cellArray,2)
var = eval(['cellArray{z,s}']);
if size(var,1) == 0
var = '';
end
if isnumeric(var) == 1
var = num2str(var);
end
fprintf(datei,var);
if s ~= size(cellArray,2)
fprintf(datei,[delimiter]);
end
end
fprintf(datei,'\n');
end
fclose(datei);
Example:
>> A = rand(5); >> B = num2cell(A); >> cell2csv('dummy.csv',B)

Simon Wang
Simon Wang 2015년 2월 25일
Thank you very much on this. Things worked execpt that if I have a cell array of strings which happen to contain a comma (e.g. "ABC company, inc"), the function split the cell into two fields - "ABC company" and "inc" while I am expecting to have just one field. Any clue on how to resolve it? I believe this is related to the use of below command:
fprintf(datei,var);
Once it sees a comma, it will treat it as a delimiter.

Konstantinos Sofos
Konstantinos Sofos 2015년 2월 26일
Hi Simon,
You could use another delimiter e.g. semicolon (";").
Initially it was to be a comma (CSV-Comma separated values), however as the comma is often used as a decimal point it wouldn't be such good separator, hence others like the semicolon.
i prefer ; since it causes less problems with decimal points, digit grouping and does not appear in much text. In windows it is dependent on the "Regional and Language Options".
Regards

Simon Wang
Simon Wang 2015년 2월 27일
Thanks! It works.

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by