How can I create an array(not a cell array) for every iteration of a for loop

5 visualizaciones (últimos 30 días)
I am writing a function that can take an n x 2 matrix 'Words' and split them up into individual 1x2 arrays. These two values are the start and end position of each word in my audio array. I have tried plotting the cell array values with
cellplot(word(1))
but this just plots a vertical line down the graph.
I also can't write these manually like 'Word1' and 'Word2' below because if there are less words than I anticipated it will return an indexing error.
So either I need a way to create arrays and not cell array per loop iteration or I need a way to plot cell arrays better
Thank you.
load Variables_REC.mat Words % Words is the array that stores the audio values
for ii = 1:height(Words)
word{ii}=Voice_Plot(Words(ii,1):Words(ii,2))
end
Word1 = cell2mat(word(1)); % This will work if I know exactly how many words there are
Word2 = cell2mat(word(2));
  1 comentario
Stephen23
Stephen23 el 23 de Ag. de 2023
Editada: Stephen23 el 23 de Ag. de 2023
"So either I need a way to create arrays ..."
Best avoided:
"...and not cell array per loop iteration"
That would be a perfectly good approach.
"I need a way to plot cell arrays better"
The CELLPLOT documentation makes it clear that it just plots a graphical representation of the shapes and types of data in the cell array. It does not plot data values. But you do not explain what you want, so we will have to guess that you want to plot data values.
Note that you can replace parentheses indexing inside CELL2MAT:
Word1 = cell2mat(word(1));
with basic and more efficient curly-brace indexing:
Word1 = word{1};

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 23 de Ag. de 2023
Editada: Stephen23 el 23 de Ag. de 2023
Fake data:
V = rand(1,99);
W = [3,9;13,17;64,91]; % [start,end]
Approach one: ARRAYFUN and CELLFUN:
F = @(b,e) V(b:e);
C = arrayfun(F,W(:,1),W(:,2),'Uni',0);
C = [cellfun(@(v)1:numel(v),C,'Uni',0),C].';
plot(C{:})
Approach two: ARRAYFUN and PADCAT:
PADCAT must be downloaded here:
F = @(b,e) V(b:e);
C = arrayfun(F,W(:,1),W(:,2),'Uni',0);
M = padcat(C{:}).';
plot(M)

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by