Displaying cells which have different sizes

2 vues (au cours des 30 derniers jours)
Yasin AHLATCI
Yasin AHLATCI le 9 Juil 2020
Commenté : Yasin AHLATCI le 9 Juil 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

Réponses (2)

madhan ravi
madhan ravi le 9 Juil 2020
for k= 1:size(Y,1)
fprintf('#1 : %s GET--> %d\n', Y{k,1}, Y{k,2})
end
  3 commentaires
madhan ravi
madhan ravi le 9 Juil 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 le 9 Juil 2020
This doesn't work well for me because for k=2 print is
'WATERCOKE' [3]
but print should be at least 'WATER' 'COKE'

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 9 Juil 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 commentaire
Yasin AHLATCI
Yasin AHLATCI le 9 Juil 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'

Connectez-vous pour commenter.

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by