dsjoyce
dsjoyce

Reputation: 3

Why is this index exceeding matrix dimensions?

I'm having trouble tweaking some matlab code, it simply graphs some data which I either accept or reject based upon some parameters, and then compiles the set of accepted data. The code below is what I am adapting, however it was set up to read data in as rows. I altered it to use data in the column format but keep hitting an Index exceeds matrix dimensions error after the final for loop, which as far as I can tell should terminate after the 10th occurrence. As a noob any guidance would be gratefully accepted!

Code is as follows:

test = rand(3,5);           % generate 5 rows of random data
accept = 0;
reject = 0;
indices = 1:size(test,2);    % initialize indices of data based on columns
figure;


for ii = 1:size(test,2)
    plot(test(:,ii));
    [x,y,button] = ginput(1);
    if button == 97 % A - for accept
        disp('Input Accepted!');
        accept = accept + 1;
    elseif button == 114 % R - for reject
        indices(ii) = 0;
       disp('Input Rejected!');
       reject = reject + 1;
    else
        disp('Button not recongnized!')
    end
    size(test,2)

end
accept  % Display number of accepted
reject  % Displey number of rejected
indices = indices(indices~=0); % Remove indices that were rejected
new_test = test(indices,:); % Create new dataset with only accepted data

Upvotes: 0

Views: 119

Answers (1)

mehmet
mehmet

Reputation: 1631

The command in the last line cause this problem. indices is responsible for cloumns.

Replace it with this:

new_test = test(:, indices);

My suggestion is that don't use command window when you have bigger than few line commands. You should create a New Script and write code to MATLAB editor. This tells you which line causes the problem when you use script so debugging would become easier.

Upvotes: 2

Related Questions