Store Data in Cell-Array
1 view (last 30 days)
Show older comments
Hello everybody,
I have a matrix with 1 row of data like you can see below and wanted to extract each block of numbers which always end before the word test, into a seperate field of a cell array. The amount of numbers can vary and after the word test are always following 5 lines of different characters.
C2=
- test
- a
- z
- a
- r
- c
- 1
- 2
- 3
- 4
- ...
- test
- a
- d
- f
- a
- t
- 45
- 36
- ...
Now I wanted to extract each block of numbers with the following code into a cell array
for i=1:length(C2)
if strcmp(C2{i,1},'Test')
g=g+1;
Cneu{g,1}=i;
end
end
lCneu=length(Cneu)
Data={}
for t=1:lCneu-1
for i=Cneu{t,1}+5:Cneu{t+1,1}-1
g=g+1
Data{t,1}{g,1}=C2{i,1};
end
end
g=0
length_C=length(C2);
for t = lCneu
for i=Cneu{t,1}+5:length_C
g=g+1
Data{t,1}{g,1}=C2{i,1};
end
end
The code stores the data in the cell array but after the first block of data the following blocks aren't stored in the first line of its cell array but in the same line where it was stored in the C2 array.
How can I get each block starting in the first line of its corresponding cell array field?
Thanks in advance
Greetings
1 Comment
Answers (1)
Andrei Bobrov
on 18 Jun 2017
variant for MATLAB R2016b and later
out = num2cell(C2(find(strcmp(C2,'test')) + (1:5)),2)
MATLAB R2016a and earlier:
out = num2cell(C2(bsxfun(@plus,find(strcmp(C2,'test')),1:5),2)
See Also
Categories
Find more on Inputs in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!