필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Assignment of a string to an empty matrix

조회 수: 1 (최근 30일)
Matt Rulli
Matt Rulli 2018년 4월 16일
마감: MATLAB Answer Bot 2021년 8월 20일
I have a script that's supposed to assign a random name from a list. The script calls on a function to pick the name, and the function works fine. When I try to assign the name, it gives me the following error: "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side." The problem is in the first FOR loop under the %% Pick Teams header: Teams{aa}(bb) = new_name; Here's my whole script>>
filename = input('Enter student list file name:','s');
team_size = input('Enter number of students per team:');
%%Import Data
names = textread(filename,'%s %*s','headerlines',1);
%%Pre-allocate Teams cell
n_names = length(names);
n_teams = round(n_names/team_size);
Teams = cell(n_teams,1);
%%Pick Teams
for aa = 1:team_size
for bb = 1:team_size
new_name = pick_names(names);
Teams{aa}(bb) = new_name;
end
end
%%Team for Extras
n_extras = rem(n_names,team_size);
for aa = 1:n_extras
new_name = pick_names(names);
Teams{n_teams+1}(aa) = new_name;
end
%%Display Teams
for aa = 1:length(names)
fprintf('Members of Team %d: \n',aa)
disp(char(Teams{aa}))
end
Any help is greatly appreciated!!
  댓글 수: 1
Bob Thompson
Bob Thompson 2018년 4월 16일
If I am not mistaken then your indexing of Teams{aa}(bb) calls a specific element of cell aa within Teams. This is acceptable, but using parentheses indicates that bb by default is a numerical or logical element, and therefore cannot receive a string. Try
Teams{aa}{bb} = new_name;

답변 (0개)

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by