Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Trouble making a matrix out of accessed data

1 回表示 (過去 30 日間)
Daniel Campbell
Daniel Campbell 2015 年 10 月 20 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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.'

回答 (1 件)

the cyclist
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

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by