How to print out a cell array inside a cell array

조회 수: 14 (최근 30일)
Garrett Linton
Garrett Linton 2019년 12월 3일
댓글: dpb 2019년 12월 4일
teampick =
1×4 cell array
{1×4 cell} {1×4 cell} {1×4 cell} {1×4 cell}

채택된 답변

dpb
dpb 2019년 12월 3일
How did you manage to get that so deeply nested? Let's see the code that generated the array and work on it...
But, to answer the question, one just keeps on dereferencing layer after layer...
>> v={{{rand(1,1)},{rand(1,1)}}} % WOW! Why would one do this????
v =
1×1 cell array
{1×2 cell}
>> v{1}{1}
ans =
1×1 cell array
{[0.4720]}
>> v{1}{1}{1}
ans =
0.4720
>>
  댓글 수: 2
Garrett Linton
Garrett Linton 2019년 12월 3일
this is why lol
team_pick_order
= [];
% Order of drafting teams
%<SM:RANDGEN>
teamdraft = randperm(num_teams) ;
%assiagn team names to draft order
%<SM:FOR>
for c1 = 1:num_teams
team_number = teamdraft(c1);
team_pick_order = [team_pick_order;teamnames(team_number), team_number];
end
%make array for the draftable player for the teams
%<SM:IF>
if num_teams == 4
teampick = cell(1,4) ;
end
if num_teams == 6
teampick = cell(1,6) ;
end
if num_teams == 8
teampick = cell(1,8) ;
end
%separte the postion to make it easy to draft
[~,~,QBs] = getcsv('QB_Table.csv') ;
[~,~,RBs] = getcsv('RB_table.csv') ;
[~,~,WRs] = getcsv('WR_table.csv') ;
[~,~,TEs] = getcsv('TE_table.csv') ;
[~,~,Ks] = getcsv('K_table.csv') ;
% Eight players on each team
number_of_rounds = 5;
% Repeat so each team gets all of its players
%<SM:FOR>
for round = 1:number_of_rounds
% What row in the draft order?
for row = 1:num_teams
% What team is drafting?
%<SM:RANDUSE>
drafting_team_number = teamdraft(1,row) ;
drafting_team_name = teamnames{teamdraft(row), 1} ;
% picks = teampicks{drafting_team_number}
% Ask the team to pick a player
fprintf('QBs\n')
fprintf('RBs\n')
fprintf('WRs\n')
fprintf('TEs\n')
fprintf('Ks\n')
fprintf('For each team draft one player from each postion.\n')
%<SM:STRING>
String = sprintf('%s please pick your postion you want to draft:', drafting_team_name);
menu = input(String, 's')
%<SM:BUILD>
%<SM:RTOTAL>
%<SM:SWITCH>
switch menu
%<SM:VALID>
case {'QBs','qbs','Qbs','qBs','qbS','QBS','qBS'}
clc;
QBs
%ask for player they want
fprintf('Copy and past player from QB cell array to avoid spelling mistakes.\n')
name = input('Draft your QB: ', 's')
%<SM:FUNCBOTH>
player_loc = lookQB(name,QBs,teampick,menu)
%if player is found place on team and take out of draft
%board
%<SM:CVAL>
%<SM:SEARCH>
if player_loc > 0
teampick{1,teamdraft(row)} = [teampick{1,teamdraft(row)} ; [QBs(player_loc, :),menu]]
QBs(player_loc, :) = [] ;
clc;
end
%<SM:VALID>
case {'RBs','rbs','Rbs','rBs','rbS','RBS','rBS'}
clc;
RBs
%ask for player they want
fprintf('Copy and past player from RB cell array to avoid spelling mistakes.\n')
name = input('Draft your RB: ', 's')
%<SM:SEARCH>
%<SM:CVAL>
player_loc = lookRB(name,RBs,teampick,menu)
%if player is found place on team and take out of draft
%board
if player_loc > 0
teampick{1,teamdraft(row)} = [teampick{1,teamdraft(row)} ;RBs(player_loc, :),menu]
RBs(player_loc, :) = [] ;
clc;
end
%<SM:VALID>
case {'WRs','wrs','Wrs','wRs','wrS','WRS','wRS'}
clc;
WRs
%ask for player they want
fprintf('Copy and past player from WR cell array to avoid spelling mistakes.\n')
name = input('Draft your WR: ', 's')
%<SM:SEARCH>
%<SM:CVAL>
player_loc = lookWR(name,WRs,teampick,menu)
%if player is found place on team and take out of draft
%board
if player_loc > 0
teampick{1,teamdraft(row)} = [teampick{1,teamdraft(row)} ;WRs(player_loc, :),menu]
WRs(player_loc, :) = [] ;
clc;
end
%<SM:VALID>
case {'TEs','tes','tEs','teS','TES','tES','Tes'}
TEs
%ask for player they want
fprintf('Copy and past player from TE cell array to avoid spelling mistakes.\n')
name = input('Draft your TE: ', 's')
%<SM:SEARCH>
%<SM:CVAL>
player_loc = lookTE(name,TEs,teampick,menu)
%if player is found place on team and take out of draft
%board
if player_loc > 0
teampick{1,teamdraft(row)} = [teampick{1,teamdraft(row)} ;TEs(player_loc, :),menu]
TEs(player_loc, :) = [] ;
clc;
end
%<SM:VALID>
case {'Ks', 'ks','KS','K','k'}
clc;
Ks
%ask for player they want
fprintf('Copy and past player from K cell array to avoid spelling mistakes.\n')
name = input('Draft your K: ', 's')
%<SM:SEARCH>
%<SM:CVAL>
player_loc = lookK(name,Ks,teampick,menu)
%if player is found place on team and take out of draft
%board
if player_loc > 0
teampick{1,teamdraft(row)} = [teampick{1,teamdraft(row)} ;Ks(player_loc, :),menu]
Ks(player_loc, :) = [] ;
clc;
end
end
end
%flip draft order so everyone has a fair chance to get a good draft pick
team_pick_order = flipud(team_pick_order);
end
dpb
dpb 2019년 12월 4일
Missing the functions being called, but when you start with cell(), then don't use the curlies "{}" for the array addressing but () to avoid inserting the cell into the cell array.
General rule is to only use cell addressing at the lowest level needed to store disparate data types; the cell array itself is just an array.
Alternatively, instead of cell arrays, consider struct with named fields to hold the disparate content of each team...the positions could be the field names.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by