MATLAB Answers

Displaying cells which have different sizes

4 views (last 30 days)
Yasin AHLATCI
Yasin AHLATCI on 9 Jul 2020
Commented: Yasin AHLATCI on 9 Jul 2020
Hi everyone. Lets say I have a cell. I tried different ways but I could'nt make it properly.
Y{1,1}=['WATER'] Y{2,1}=2
Y{2,1}=['WATER' 'COKE'] Y{2,2}=3
my code to display these are ;
a=0;
while (a<length(Y))
a=a+1;
disp(['#' num2str(a) ':' (Y{a,1}) ' GET-->' mat2str(Y{a,2})])
end
RESULT
'#' '1' ':' 'WATER' 'GET-->' '2'
'#' '2' ':' 'WATER' 'COKE' 'GET-->' '3'
How can I get something like that
#1 : 'WATER' GET--> 2
#1 : 'WATER' 'COKE' GET--> 3

  0 Comments

Sign in to comment.

Answers (2)

madhan ravi
madhan ravi on 9 Jul 2020
for k= 1:size(Y,1)
fprintf('#1 : %s GET--> %d\n', Y{k,1}, Y{k,2})
end

  3 Comments

Yasin AHLATCI
Yasin AHLATCI on 9 Jul 2020
I have an error ;
Error using fprintf
Function is not defined for 'cell' inputs.
madhan ravi
madhan ravi on 9 Jul 2020
clear all
Y{1,1}=['WATER']
Y{1,2}=2
Y{2,1}=['WATER', 'COKE']
Y{2,2}=3
for k= 1:size(Y,1)
s = repmat('%s', 1, numel(Y{k,1}));
fprintf('#1 :',s,' GET--> %d\n', Y{k,1}, Y{k,2})
end
Yasin AHLATCI
Yasin AHLATCI on 9 Jul 2020
This doesn't work well for me because for k=2 print is
'WATERCOKE' [3]
but print should be at least 'WATER' 'COKE'

Sign in to comment.


Image Analyst
Image Analyst on 9 Jul 2020
Try this:
Y = {1, 'WATER', []; ...
1, 'WATER', 'COKE'}
You get:
Y =
2×3 cell array
{[1]} {'WATER'} {0×0 double}
{[1]} {'WATER'} {'COKE' }
The 1,3 cell is still there since you can't have missing elements in an array. Like any array, cell arrays included, you can't have 2 columns in the first row and 3 columns in the second row.

  1 Comment

Yasin AHLATCI
Yasin AHLATCI on 9 Jul 2020
Hello. I think I misrepresented my situation. My cell is like that. (1x2 cell is 'WATER' 'COKE')
I tried different things for printing it but I couldn't make it. a is my counter.
while (a<size(Y,1))
a=a+1;
disp(['#' num2str(a) ':' mat2str(Y{a,1}) ' GET->' mat2str(Y{a,2})]);
end
This works for a=1 Print is;
#1:'WATER' GET->2
But for a=2 I get an error because Y{2,1} is (1x2) cell.
When I didn't use mat2str for Y{a,1} Print is ;
'#' '2' ':' 'WATER' 'COKE' ' GET->' '3'

Sign in to comment.

Tags