Accessing matrix columns using index which are not continuous (example:index=1,5,9,13 etc)

2 views (last 30 days)
I have a matrix say R which is having columns corresponding to some variable & text files each of which also contains a column.I have to read these text files corresponding to each column of the matrix R which are not in sequence.For example I have to read text file 1.txt for columns 1,5,7,8 and 2.txt for 2,9,11,14 .How can I do it using for or some other loop? Thanks in advance.

Answers (1)

Guillaume
Guillaume on 23 Oct 2015
I'm not clear on what exactly is posing you problem and not clear either on the structure of your text files, so can only give you generic advice:
Assumption: you have a cell array, let's call it columnorder, which tell you the origin of each column of R. e.g.:
columnorder = {[1 5 7 8], [2 9 11 14], [3 4 6 10 12 13]};
tells you that column 1 5 7 8 come from column 1:4 pf the first file, 2 9 11 14 come from column 1:4 of the second file and column 3 10 4 12 6 13 come from column 1:6 of the third file. Note that I shuffled the order of the columns from that third file on purpose.
assert(sort([columnorder{:}]) == 1:max([columnorder{:}]), 'some columns are missing or duplicate');
R = zeros(someheight, max([columnorder{:}])); %destination
for fileidx = 1:numtextfile
filedata = csvread(sprintf('%d.txt', fileidx)); %or however you read the file
R(:, columnorder{fileidx}) = filedata(:, 1:numel(columnorder{fileidx}));
end

Community Treasure Hunt

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

Start Hunting!