Info
此问题已关闭。 请重新打开它进行编辑或回答。
Trouble making a matrix out of accessed data
1 次查看(过去 30 天)
显示 更早的评论
I have a 1x80 structure with a field called 'Task'.
The 'Task' field can either be a or b.
I want to construct a TaskType matrix that is 1x80, that basically says
if Task=='a', then make the corresponding matrix value 1.
if Task=='b', then make the corresponding matrix value 2.
if neither, then make the matrix value 3.
Here's my code so far:
for x=1:length(Structure)
if Structure(x).Task=='a'
TaskType(x)=1;
elseif Structure(x).Task=='b'
TaskType(x)=2;
else
TaskType(x)=3;
end
end
My code is returning 'matrix dimensions must agree.'
0 个评论
回答(1 个)
the cyclist
2015-10-20
This test worked just fine for me:
% Fill in random tasks
elements = {'a','b','c','fudge'};
tasks = elements(randi(length(elements),[1,80]));
Structure = struct('Task',tasks);
% Apply Daniel's algorithm
for x=1:length(Structure)
if Structure(x).Task=='a'
TaskType(x)=1;
elseif Structure(x).Task=='b'
TaskType(x)=2;
else
TaskType(x)=3;
end
end
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!