slicing an array of lines
5 views (last 30 days)
Show older comments
Hello, i have a 1X1022 array of strings which i took from TXT file tried to put every 71'th row in separated array ,i tried to do it with two methods in both of them i couldnt achieve the goal, if you could tell what could i do thanks 1. in this method i get an error
Subscript indices must either be real positive integers or logicals.
Error in Untitled (line 6) y{1}(k)= g{1}(i)
fid=fopen('test.txt');
g = textscan(fid,'%s','delimiter','\n');
fclose(fid)
k=0
for i=4:length(g{1}):71
y{1}(k)= g{1}(i)
k=k+1
end
i got only the first line 2. by
for i=4:71:length(g{1})
freq{1}{i}=g{1}{i};
end
i got a blank
0 Comments
Accepted Answer
Guillaume
on 16 Feb 2017
The increment goes in between the bounds, so it's
4:71:numel(g{1}) %numel is safer than length
You don't need a loop to slice an array. Just pass the required indices directly. Furthermore, since the cell array is only one cell, why not get rid of it entirely as you seem to be struggling with it anyway.
lines = g{1}; %get rid of the 1-cell cell array
freq = lines(4:71:end); %when indexing you can use end instead of numel or length. It's even easier.
%or as one line:
freq = g{1}(4:71:end);
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!