Displaying cells which have different sizes
Show older comments
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
Answers (2)
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
on 9 Jul 2020
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
on 9 Jul 2020
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
on 9 Jul 2020
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!