slicing an array of lines

3 ビュー (過去 30 日間)
fima v
fima v 2017 年 2 月 16 日
回答済み: Guillaume 2017 年 2 月 16 日
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

採用された回答

Guillaume
Guillaume 2017 年 2 月 16 日
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 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by