Clear Filters
Clear Filters

Could anyone help me how to split the {1x3 cell}

3 views (last 30 days)
I want to split {1x3 cell} into three groups. Could anyone please help me on this.
  12 Comments
Walter Roberson
Walter Roberson on 11 Sep 2018
If you are unable to even comment out the sections I explicitly gave the lines of to comment out in https://www.mathworks.com/matlabcentral/answers/417768-could-anyone-help-me-to-overcome-the-error#comment_607400 then you need to hire a tutor to teach you how to use the MATLAB editor.
We are not here to do your work for you.
Remember this about programming: get it working first, and then worry about making it compact or efficient or beautiful.
You need to know one key fact about MATLAB before you start: namely that when you use fprintf() then if you do not emit a newline or carriage return, then the next output will continue on at the end of the current line. So if you need to print two different things on the same line, you can do it by using fprintf() of the first one and then fprintf() of the second one, and finally when you decide you have nothing more to print on the line, you can fprintf('\n') to advance to the next line.
Beyond that, to display a list of data on a line, you can use this strategy:
1) emit the fixed leading part of the line
2) emit whatever mark you are using to signal the beginning of the list in the output
3) start a loop over the elements to be displayed in the list
3A) display one element
3B) If you have not just displayed the last of the elements, emit whatever characters you are using between elements, such as space or comma space or tab or semi-colon
3C) end of loop
4) emit whatever mark you are using to signal the end of the list in the output
5) if there is anything else to display on the line, emit it
6) emit newline
Calling fprintf() multiple times per line is often not the most efficient thing you could do, but it gets the job done.
As you get more accustomed to MATLAB you will find that there are often ways to display more data in one fprintf() call.
In the case of a displaying a purely numeric table with the same number of elements per line, this can often be done quite easily if the number of columns is fixed, and with just one or two more lines of preparation for the case where the number of columns is not fixed. This does not happen to be the situation you have at the moment: you are displaying a different number of entries each time.
In the case where the number of entries per line varies, or where the entries are not all numeric, then there are approaches that can be taken in MATLAB to display a lot of information in single calls to fprintf(), but those methods are more advanced and more difficult to read the code of.
I would not recommend that you try creating the advanced displays at the moment: just write loops (and perhaps nested loops) to do the displaying in simple ways. As you learn more MATLAB you can always go back later to make the code more efficient.
jaah navi
jaah navi on 12 Sep 2018
I have commented out the lines which you have mentioned but i am unable to get the result even after commenting out the lines. code:
function [] = partdisp(A,K)
if nargin>2
error('A maximum of two input argument is allowed.')
end
if ~iscell(A)
error('This function is only useful on the output from PARTITIONS.')
end
L = length(A);
fprintf('\n')
if iscell(A{1}{1})
if ischar(A{1}{1}{1})
f = '%s ';
if nargin==1
fprintf('%s',['The ',num2str(L),' partitions of set {'])
for ii = 1:length(A{1})
for jj = 1:length(A{1}{ii})
fprintf('%s ',A{1}{ii}{jj})
end
end
fprintf('\b%s\n','}:')
else
fprintf('%s',['The ',num2str(L),' partitions of set {'])
for ii = 1:length(A{1})
for jj = 1:length(A{1}{ii})
fprintf('%s ',A{1}{ii}{jj})
end
end
fprintf('\b%s\n',['} of length ',num2str(K),':'])
end
else
f = '%g
end
for ii = 1:length(A)
for jj = 1:length(A{ii})
for kk = 1:length(A{ii}{jj})
fprintf(f,A{ii}{jj}{kk})
end
fprintf('\b} ')
end
fprintf('\n')
end
else
if ischar(A{1}{1})
f = '%s ';
if nargin==1
disp(['The ',num2str(L),' partitions of set {',[A{1}{:}],'}:'])
else
disp(['The ',num2str(L),' partitions of set {', [A{1}{:}],...
'} of length ',num2str(K),':'])
end
else
f = '%g ';
if nargin==1
disp(['The ',num2str(L),' partitions of set {',...
num2str([A{1}{:}],'%g '), '}:'])
else
disp(['The ',num2str(L),' partitions of set {',...
num2str([A{1}{:}],'%g '), '} of length ',num2str(K),':'])
end
end
for ii = 1:length(A)
for jj = 1:length(A{ii})
fprintf('{')
fprintf(f,A{ii}{jj})
fprintf('\b} ')
end
fprintf('\n')
end
end
fprintf('\n')
end

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Sep 2018
function [] = partdisp(A,K)
if nargin>2
error('A maximum of two input argument is allowed.')
end
if ~iscell(A)
error('This function is only useful on the output from PARTITIONS.')
end
L = length(A);
fprintf('\n')
if iscell(A{1}{1})
if ischar(A{1}{1}{1})
f = '%s ';
else
f = '%g ';
end
for ii = 1:length(A)
for jj = 1:length(A{ii})
for kk = 1:length(A{ii}{jj})
fprintf(f,A{ii}{jj}{kk})
end
fprintf('\b ')
end
fprintf('\n')
end
else
if ischar(A{1}{1})
f = '%s ';
else
f = '%g ';
end
for ii = 1:length(A)
for jj = 1:length(A{ii})
fprintf(f,A{ii}{jj})
fprintf('\b ')
end
fprintf('\n')
end
end
fprintf('\n')
  9 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!