1 function csvwrite(fileName,inpCMat,varargin)
7 checkvar(inpCMat,
'ismatrix(x)&&(iscell(x)||isnumeric(x))');
9 [~,~,delimStr,columnNameList,numFormatStr,~,isColumnNameListSpec]=...
11 {
'delimiter',
'columnNameList',
'numFormat';...
13 'isscalar(x)&&ischar(x)',
'iscellofstring(x)&&isrow(x)',...
15 if isColumnNameListSpec
16 checkmultvar(
'numel(x1)==size(x2,2)',2,columnNameList,inpCMat);
19 [fid,messageStr] = fopen(fileName,
'w');
22 [
'cannot create file %s for writing, reason:',messageStr],...
26 %% We assume that values in each column has
27 nCols=size(inpCMat,2);
33 if isColumnNameListSpec
34 fprintf(fid,[
'"%s"',
repmat(
',"%s"',1,nCols-1),
'\n'],...
38 isCharVec=cellfun(
'isclass',inpCMat(1,:),
'char');
39 isNumericVec=cellfun(@isnumeric,inpCMat(1,:));
41 if ~all(isCharVec|isNumericVec)
42 isBadVec=~(isCharVec|isNumericVec);
44 [
'only char and numeric types are supported, ',...
45 'columns %s have unsupported type'],mat2str(find(isBadVec)));
48 isCharMat=cellfun(
'isclass',inpCMat(:,isCharVec),
'char');
51 isNumericMat=cellfun(@isnumeric,inpCMat(:,isNumericVec));
54 formatStrList=cell(1,nCols);
55 formatStrList(isCharVec)={
'"%s"'};
56 formatStrList(isNumericVec)={numFormatStr};
62 fprintf(fid,formatStr,inpCMat{:}); 70 function checkTypeOfAllRows(isNeededTypeMat,typeName) 71 isColOfNeededType=all(isNeededTypeMat,1); 72 if ~all(isColOfNeededType) 73 indCharColVec=find(isCharVec); 74 throwerror('wrongInput:differentTypesOfRows
',... 75 'not all rows in columns %s have %s type
',... 76 mat2str(indCharColVec(~isColOfNeededType)),typeName); function num2cell(in inpArray, in varargin)
NUM2CELL is an extension of Matlab built-in function "num2cell" designed to work correctly with empty...
function throwerror(in msgTag, in varargin)
THROWERROR works similarly to built-in ERROR function in case when there is no output arguments but s...
function checkmultvar(in typeSpec, in nPlaceHolders, in varargin)
CHECKMULTVAR checks a generic condition provided by typeSpec string in the following format: 'isnumer...
function repmat(in inpArray, in varargin)
function checkvar(in x, in typeSpec, in varargin)
CHECKVAR checks a generic condition provided by typeSpec string in the following format: 'isnumeric(x...
function checkTypeOfAllRows(in isNeededTypeMat, in typeName)
function csvwrite(in fileName, in inpCMat, in varargin)
CSVWRITE writes a specified cell matrix into a comma-separated file specified by name. All columns of the matrix are expected to be of the same type. As of the moment only 'char' and all numeric types are supported.