Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

Trouble making a matrix out of accessed data

1 visualizzazione (ultimi 30 giorni)
Daniel Campbell
Daniel Campbell il 20 Ott 2015
Chiuso: MATLAB Answer Bot il 20 Ago 2021
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.'

Risposte (1)

the cyclist
the cyclist il 20 Ott 2015
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

Questa domanda è chiusa.

Community Treasure Hunt

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

Start Hunting!

Translated by