Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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
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)
Word1 = cell2mat(word(1)); % This will work if I know exactly how many words there are
Word2 = cell2mat(word(2));
  1 Commento
Stephen23 il 23 Ago 2023
Modificato: Stephen23 il 23 Ago 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};

Accedi per commentare.

Risposta accettata

Stephen23 il 23 Ago 2023
Modificato: Stephen23 il 23 Ago 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].';
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{:}).';

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by