forked from GRAP-UdL-AT/fruit_detection_in_LiDAR_pointClouds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxt2cell.m
39 lines (37 loc) · 1.23 KB
/
txt2cell.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function cell = txt2cell(textfile, retreive_by, idxs)
% Function that reads a textfile and returns a cell array with the
% indicated columns. If there is no indicated columns the function returns
% the whole textfile into a cell array.
% Example: txt2cell(textfile) returns all columns and rows.
% txt2cell(textfile, 'columns', [1 3 5]) returns the first, third and fifth
% columns (in the specified order).
% txt2cell(textfile, 'rows', [1 3 5]) returns the first, third and
% fifth rows (in the specified order).
if nargin < 2
retreive_by = 'nan';
idxs = 0;
end
if nargin == 2
disp('Error, requires index of selected columns or rows')
return
end
file = fopen(textfile);
cell = [];
while(1)
row = fgetl(file);
if(row == -1)
break
else
split_row = strsplit(row);
if (isequal(retreive_by, 'columns'))
cell = [cell; split_row(idxs)]; %Return selected columns
else
cell = [cell; split_row]; %Return the whole txt
end
end
end
fclose(file);
if strcmp(retreive_by, 'rows')
cell = cell(idxs, :);
end
end