MatrixBerryCore
showcell.m
Go to the documentation of this file.
1 function outStr=showcell(inpCArr,varargin)
2 import mxberry.core.throwerror;
3 if ~iscell(inpCArr)
4  throwerror('wrongInput','This is not a cell array');
5 end
6 %
7 [~,~,nSpaces,nDigits,isVarNamePrinted,varName,isClassShown,...
8  isAsExpression,nMaxShownArrayElems,...
9  ~,isNDigitsSpec,~,isVarNameSpec]=...
10  mxberry.core.parseparext(varargin,...
11  {'nSpaces','nDigits','printVarName','varName','showClass',...
12  'asExpression','nMaxShownArrayElems';...
13  4,15,false,[],false,false,10;...
14  'isnumeric(x)&&isscalar(x)','isnumeric(x)&&isscalar(x)',...
15  'islogical(x)&&isscalar(x)','isstring(x)',...
16  'islogical(x)&&isscalar(x)','islogical(x)&&isscalar(x)',...
17  'isnumeric(x)&&isscalar(x)&&(fix(x)==x)&&(x>=0)'},0);
18 %
19 if isNDigitsSpec
20  fNumPrint=@(x)numArr2str(x,isAsExpression,isClassShown,nDigits);
21 else
22  fNumPrint=@(x)numArr2str(x,isAsExpression,isClassShown);
23 end
24 fCharPrint=@(x)charArr2str(x,isAsExpression);
25 fCellPrint=@(x)mxberry.core.cell.showcell(x,varargin{:});
26 %
27 if ~isVarNameSpec&&isVarNamePrinted
28  varName=inputname(1);
29  if isempty(varName)
30  varName='ans';
31  end
32 end
33 %
34 sizeVec=size(inpCArr);
35 areAllFirstDimsZeros=max(sizeVec)==0;
36 areAnyFirstDimsZeros=min(sizeVec)==0;
37 %
38 nRestDims=ndims(inpCArr) - 2;
39 %
40 if nRestDims==0&&areAllFirstDimsZeros
41  if isAsExpression
42  lineStrList={'cell.empty(0,0)'};
43  else
44  lineStrList={'{}'};
45  end
46 else
47  if isAsExpression
48  if areAnyFirstDimsZeros
49  lineStrList={getEmptyStr(inpCArr)};
50  else
51  lineStrList=showCellInternal(inpCArr,nSpaces,fNumPrint,...
52  fCharPrint,fCellPrint,...
53  true,nMaxShownArrayElems);
54  end
55  else
56  if areAnyFirstDimsZeros
57  dimList=arrayfun(@(x)sprintf('%d',x),sizeVec,...
58  'UniformOutput',false);
59  lineStrList={sprintf('Empty cell array: %s\n',...
60  mxberry.core.string.catwithsep(dimList,'-by-'))};
61  else
62  if (nRestDims>0)
63  indDimVecList=cell(nRestDims,1);
64  %
65  nHigherDimElems=prod(sizeVec(3:end));
66  %
67  [indDimVecList{:}]=ind2sub(sizeVec(3:end),...
68  transpose(1:nHigherDimElems));
69  indMat=horzcat(indDimVecList{:});
70  indElemVecList=mat2cell(indMat,...
71  ones(1,nHigherDimElems),nRestDims);
72  %
73  isElemNumPrinted=true;
74  else
75  nHigherDimElems=1;
76  isElemNumPrinted=false;
77  end
78  %
79  listOflineStrList=cell(1,nHigherDimElems);
80  for iHigherDimElem=1:nHigherDimElems
81  curPartCMat=inpCArr(:,:,iHigherDimElem);
82  %
83  lineStrList=showCellInternal(curPartCMat,nSpaces,fNumPrint,...
84  fCharPrint,fCellPrint,false,nMaxShownArrayElems);
85  if isElemNumPrinted
86  elemNumStr=['(:,:',sprintf(',%d',...
87  indElemVecList{iHigherDimElem}),')'];
88  nSymbols=numel(elemNumStr);
89  blankStubStr=repmat(' ',1,nSymbols);
90  linePrefixList=cell(size(lineStrList));
91  linePrefixList{1}=elemNumStr;
92  if numel(linePrefixList)>1
93  linePrefixList{2:end}=blankStubStr;
94  end
95  lineStrList=strcat(linePrefixList,lineStrList);
96  end
97  listOflineStrList{iHigherDimElem}=lineStrList;
98  end
99  %
100  lineStrList=vertcat(listOflineStrList{:});
101  end
102  end
103 end
104 if isVarNamePrinted
105  linePrefix=sprintf('%s=\n',varName);
106  lineStrList=vertcat(linePrefix,lineStrList);
107 end
108 if nargout==0
109  fprintf('%s\n',lineStrList{:});
110 else
111  outStr=mxberry.core.string.catwithsep(lineStrList,sprintf('\n'));
112 end
113 %%
114 
115 function lineStrList=showCellInternal(inpCMat,nSpaces,fNumPrint,fCharPrint,fCellPrint,isAsExpression,nMaxShownArrayElems)
116 inpCVec=inpCMat(:);
117 isCellVec=cellfun('isclass',inpCVec,'cell');
118 isLogicalVec=cellfun('isclass',inpCVec,'logical');
119 isCharVec=cellfun('isclass',inpCVec,'char');
120 [~,isNumericVec]=mxberry.core.iscellnumeric(inpCVec);
121 %
122 if isAsExpression
123  isSpecialEmptyVec=isCharVec|isCellVec|isNumericVec;
124  isEmptyVec=cellfun('isempty',inpCVec)&...
125  (cellfun(@(x)any(size(x)>0),inpCVec)&isSpecialEmptyVec|...
126  ~isSpecialEmptyVec);
127 else
128  isEmptyVec=false(size(inpCVec));
129 end
130 %%
131 isLessElemVec=cellfun(@(x)(numel(x)<=nMaxShownArrayElems),inpCVec);
132 isExprNumVec=(isNumericVec|isLogicalVec)&isLessElemVec&~isEmptyVec;
133 %%
134 isCharVec=isCharVec&isLessElemVec&~isEmptyVec;
135 isCellVec=isCellVec&isLessElemVec&~isEmptyVec;
136 %%
137 isOtherVec=~(isEmptyVec|isExprNumVec|isCharVec|isCellVec);
138 %
139 %% Deal with empty elements
140 inpCVec(isEmptyVec)=cellfun(@getEmptyStr,inpCVec(isEmptyVec),...
141  'UniformOutput',false);
142 %
143 %% Deal with cell elements
144 inpCVec(isCellVec)=cellfun(fCellPrint,inpCVec(isCellVec),...
145  'UniformOutput',false);
146 %% Deal with numeric elements
147 inpCVec(isExprNumVec)=cellfun(fNumPrint,...
148  inpCVec(isExprNumVec),'UniformOutput',false);
149 %% Deal with string elements
150 % Put single quotes around the strings
151 inpCVec(isCharVec)=cellfun(fCharPrint,inpCVec(isCharVec),...
152  'UniformOutput',false);
153 %% Deal with elements other than string or numeric
154 % --------------------------------------------------------------------------
155 indOtherVec=find(isOtherVec);
156 nOtherElems=numel(indOtherVec);
157 %
158 for iObj=1:nOtherElems
159  indOther=indOtherVec(iObj);
160  valueObj=inpCVec{indOther};
161  valSizeVec=size(valueObj);
162  valNDims=ndims(valueObj);
163  className=class(valueObj);
164  %
165  %% Display size and class information
166  %
167  isEnum=isenum(valueObj);
168  isScalar=isscalar(valueObj);
169  if isEnum&&isScalar
170  inpCVec{indOther}=sprintf('%s',char(valueObj));
171  else
172  highDimFormat='%d-D %s';
173  lowDimFormat='%dx%d %s';
174  %
175  if valNDims==2
176  inpCVec{indOther}=sprintf(lowDimFormat,valSizeVec(1),...
177  valSizeVec(2),className);
178  else
179  inpCVec{indOther}=sprintf(highDimFormat,valNDims,className);
180  end
181  end
182 end
183 %% Reconstruct the original size
184 inpCMat=reshape(inpCVec,size(inpCMat));
185 %% Transform a cell array of strings into a cell vector of line strings
186 if isAsExpression
187  lineStrList={mxberry.core.cell.cellstr2expression(inpCMat,false,true)};
188 else
189  lineStrList=mxberry.core.cell.cell2tablestr([],inpCMat,' ',...
190  'minSepCount',nSpaces,'UniformOutput',false);
191 end
192 %%
193 
194 function resStr=other2str(inpVal)
195  valSizeVec=size(inpVal);
196  valNDims=ndims(inpVal);
197  className=class(inpVal);
198  %
199  %% Display size and class information
200  %
201  isEnum=isenum(inpVal);
202  isScalar=isscalar(inpVal);
203  if isEnum&&isScalar
204  resStr=sprintf('%s',char(inpVal));
205  else
206  highDimFormat='%d-D %s';
207  lowDimFormat='%dx%d %s';
208  %
209  if valNDims==2
210  resStr=sprintf(lowDimFormat,valSizeVec(1),...
211  valSizeVec(2),className);
212  else
213  resStr=sprintf(highDimFormat,valNDims,className);
214  end
215  end
216 function resStr=numArr2str(inpVal,~,isClassShown,varargin)
217 origSizeVec=size(inpVal);
218 if max(origSizeVec)==0
219  if isClassShown
220  resStr=[class(inpVal),'([])'];
221  else
222  resStr='[]';
223  end
224 else
225  if isClassShown
226  inpArgList={'class'};
227  else
228  inpArgList={};
229  end
230  isReshaped=~ismatrix(inpVal);
231  if isReshaped
232  inpVal=inpVal(:,:);
233  end
234  resStr=mat2str(inpVal,varargin{:},inpArgList{:});
235  if isReshaped
236  resStr=['reshape(',resStr,',',mat2str(origSizeVec),')'];
237  end
238 end
239 function inpVal=charArr2str(inpVal,isAsExpression)
240 origSizeVec=size(inpVal);
241 if max(origSizeVec)==0
242  inpVal='''''';
243 else
244  isReshaped=~isrow(inpVal);
245  if isAsExpression
246  if isReshaped
247  inpVal=inpVal(:).';
248  end
249  inpVal=['''',strrep(inpVal,'''',''''''),''''];
250  if isReshaped
251  inpVal=['reshape(',inpVal,',',mat2str(origSizeVec),')'];
252  end
253  else
254  if isReshaped
255  inpVal=other2str(inpVal);
256  else
257  inpVal=['''',inpVal,''''];
258  end
259  end
260 end
261 %%
262 
263 function resStr=getEmptyStr(x)
264 sizeVec=size(x);
265 resStr=[class(x),'.empty(',sprintf('%d,',sizeVec(1:end-1)),...
266  sprintf('%d',sizeVec(end)),')'];
function other2str(in inpVal)
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 repmat(in inpArray, in varargin)
function showcell(in inpCArr, in varargin)
SHOWCELL generates a string representation of cell array or displays it in a console.
function charArr2str(in inpVal, in isAsExpression)
function showCellInternal(in inpCMat, in nSpaces, in fNumPrint, in fCharPrint, in fCellPrint, in isAsExpression, in nMaxShownArrayElems)
function numArr2str(in inpVal, in ignoredArg, in isClassShown, in varargin)
function getEmptyStr(in x)
function isrow(in inpArray)