How can I create an array(not a cell array) for every iteration of a for loop
조회 수: 4 (최근 30일)
이전 댓글 표시
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
Stephen23
2023년 8월 23일
편집: Stephen23
2023년 8월 23일
"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};
채택된 답변
Stephen23
2023년 8월 23일
편집: Stephen23
2023년 8월 23일
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)
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!