MatrixBerryCore
csvwrite.m
Go to the documentation of this file.
1 function csvwrite(fileName,inpCMat,varargin)
2 import mxberry.core.checkvar;
3 import mxberry.core.checkmultvar;
4 import mxberry.core.throwerror;
5 %
6 checkvar(fileName,'isstring(x)');
7 checkvar(inpCMat,'ismatrix(x)&&(iscell(x)||isnumeric(x))');
8 %
9 [~,~,delimStr,columnNameList,numFormatStr,~,isColumnNameListSpec]=...
10  mxberry.core.parseparext(varargin,...
11  {'delimiter','columnNameList','numFormat';...
12  ',',[],'%10.10g';...
13  'isscalar(x)&&ischar(x)','iscellofstring(x)&&isrow(x)',...
14  'isstring(x)'},0);
15 if isColumnNameListSpec
16  checkmultvar('numel(x1)==size(x2,2)',2,columnNameList,inpCMat);
17 end
18 %
19 [fid,messageStr] = fopen(fileName,'w');
20 if fid<0
21  mxberry.core.throwerror('failedToOpenFile',...
22  ['cannot create file %s for writing, reason:',messageStr],...
23  datName);
24 end
25 %
26 %% We assume that values in each column has
27 nCols=size(inpCMat,2);
28 try
29  if nCols>0
30  if isnumeric(inpCMat)
31  inpCMat=num2cell(inpCMat);
32  end
33  if isColumnNameListSpec
34  fprintf(fid,['"%s"',repmat(',"%s"',1,nCols-1),'\n'],...
35  columnNameList{:});
36  end
37  %
38  isCharVec=cellfun('isclass',inpCMat(1,:),'char');
39  isNumericVec=cellfun(@isnumeric,inpCMat(1,:));
40  %
41  if ~all(isCharVec|isNumericVec)
42  isBadVec=~(isCharVec|isNumericVec);
43  throwerror('wrongInput:wrongDataType',...
44  ['only char and numeric types are supported, ',...
45  'columns %s have unsupported type'],mat2str(find(isBadVec)));
46  end
47  %
48  isCharMat=cellfun('isclass',inpCMat(:,isCharVec),'char');
49  checkTypeOfAllRows(isCharMat,'char')
50  %
51  isNumericMat=cellfun(@isnumeric,inpCMat(:,isNumericVec));
52  checkTypeOfAllRows(isNumericMat,'numeric')
53  %
54  formatStrList=cell(1,nCols);
55  formatStrList(isCharVec)={'"%s"'};
56  formatStrList(isNumericVec)={numFormatStr};
57  %
58  formatStr=[mxberry.core.string.catwithsep(formatStrList,delimStr),...
59  '\n'];
60  %
61  inpCMat=inpCMat.';
62  fprintf(fid,formatStr,inpCMat{:});
63  end
64  fclose(fid);
65 catch meObj
66  fclose(fid);
67  rethrow(meObj);
68 end
69 end
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);
77 end
78 end
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: &#39;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: &#39;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 &#39;char&#39; and all numeric types are supported.