Displaying cells which have different sizes

조회 수: 3 (최근 30일)
Yasin AHLATCI
Yasin AHLATCI 2020년 7월 9일
댓글: Yasin AHLATCI 2020년 7월 9일
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

답변 (2개)

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

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2020년 7월 9일
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
Yasin AHLATCI
Yasin AHLATCI 2020년 7월 9일
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'

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by