Results 1 to 2 of 2

Math Help - Index exceeds matrix dimensions.

  1. #1
    Newbie
    Joined
    Apr 2010
    Posts
    1

    Index exceeds matrix dimensions.

    I'm deleting a row from a matrix then writing it to a text file and am getting an error
    ------
    ??? Index exceeds matrix dimensions.

    Error in ==> final_program at 125
    fprintf(fid,'%s %s %s %s',guests{i, 1},guests{i, 2},guests{i, 3},guests{i, 4});
    ------
    The matrix is good and I have checked it in MATLAB and it displays exactly how it should. I have this snippet of code working correctly in another switch statement when it needs to replace a cell array and then print to text file but once I try to use it to remove the cell row and print I get this annoying error. Thanks!

    Here's the snippet of code in which it occurs.
    Code:
     
    % delete row from text file
                            [guests, lines] = show_guests('hotel.txt');
                            choices = guests;
                            choices(:,2:4) = [];
                            choices = choices';
                            [reservations, hh] = listdlg('ListString',choices, 'SelectionMode','single');
                            if hh == 1 
                                % create the new data file
                                guests(reservations, :) = [];
                                
                                % write the changes to text file
                                fid = fopen('hotel.txt', 'w+');
                                i=1;
                                for j=i:lines
                                    if i ~= lines
                                        fprintf(fid,'%s %s %s %s\n',guests{i, 1},guests{i, 2},guests{i, 3},guests{i, 4});
                                    else
                                        fprintf(fid,'%s %s %s %s',guests{i, 1},guests{i, 2},guests{i, 3},guests{i, 4});
                                    end
                                    i = i+1;
                                end
                                fclose(fid);
                            end
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by thisisalex90 View Post
    I'm deleting a row from a matrix then writing it to a text file and am getting an error
    ------
    ??? Index exceeds matrix dimensions.

    Error in ==> final_program at 125
    fprintf(fid,'%s %s %s %s',guests{i, 1},guests{i, 2},guests{i, 3},guests{i, 4});
    ------
    The matrix is good and I have checked it in MATLAB and it displays exactly how it should. I have this snippet of code working correctly in another switch statement when it needs to replace a cell array and then print to text file but once I try to use it to remove the cell row and print I get this annoying error. Thanks!

    Here's the snippet of code in which it occurs.
    Code:
     
    % delete row from text file
                            [guests, lines] = show_guests('hotel.txt');
                            choices = guests;
                            choices(:,2:4) = [];
                            choices = choices';
                            [reservations, hh] = listdlg('ListString',choices, 'SelectionMode','single');
                            if hh == 1 
                                % create the new data file
                                guests(reservations, :) = [];
     
                                % write the changes to text file
                                fid = fopen('hotel.txt', 'w+');
                                i=1;
                                for j=i:lines
                                    if i ~= lines
                                        fprintf(fid,'%s %s %s %s\n',guests{i, 1},guests{i, 2},guests{i, 3},guests{i, 4});
                                    else
                                        fprintf(fid,'%s %s %s %s',guests{i, 1},guests{i, 2},guests{i, 3},guests{i, 4});
                                    end
                                    i = i+1;
                                end
                                fclose(fid);
                            end
    It may not be your problem but you should not be modifying the lower limit of a for loop inside the loop.

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Question concerning matrix dimensions.
    Posted in the Advanced Algebra Forum
    Replies: 1
    Last Post: May 21st 2011, 02:49 PM
  2. Replies: 0
    Last Post: May 12th 2011, 02:37 PM
  3. Replies: 0
    Last Post: July 26th 2010, 01:33 PM
  4. Rotating a matrix in n dimensions
    Posted in the Advanced Algebra Forum
    Replies: 1
    Last Post: January 8th 2010, 01:55 AM
  5. TI-89: Can't adjust matrix dimensions
    Posted in the Calculators Forum
    Replies: 3
    Last Post: July 1st 2008, 11:17 AM

Search Tags


/mathhelpforum @mathhelpforum