1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab - Index exceeds matrix dimensions when writing .txt

Tags:
  1. Dec 18, 2014 #1
    1. The problem statement, all variables and given/known data
    Index exceeds matrix dimensions

    2. Relevant equations


    3. The attempt at a solution
    I am trying to output into a text file something like this:


    Code (Text):

    Name                                  ID   scE   ccE   scC   ccC
    Fake Subject 1                    1      3       4       5      2
    Fake Subject 2                    1      4       3       5      2
    Fake Subject 3                    1      5       2       5      2
     
    (EDIT: My apologies, it isn't outputting correctly)

    ...
    Attempt:

    Please note that uData is a 27x5 matrix, and name_Database is a 27x1 cell array.


    Code (Text):

    ID = dbedit.uData(:,1);
    scE = dbedit.uData(:,2);
    ccE = dbedit.uData(:,3);
    scC = dbedit.uData(:,4);
    ccC = dbedit.uData(:,5);
    names = dbedit.name_Database;


    % Create array versions to account for proper spacing
    % within the text file.
    ID_cell = cellstr(num2str(ID.'));
    scE_cell = cellstr(num2str(scE.'));
    ccE_cell = cellstr(num2str(ccE.'));
    scC_cell = cellstr(num2str(scC.'));
    ccC_cell = cellstr(num2str(ccC.'));


    output_file = 'uDatabase.txt'; % Text file to output data into.
    fid = fopen(output_file, 'w+'); %// open file for writing
    fprintf(fid, 'Name\t ID\t scE\t ccE\t scC\t ccC\n'); % Header

    for ii=1:numel(names)
        fprintf(fid, '%s\t %s\t %s\t %s\t %s\t %s\n',names{ii},...
        ID_cell{ii},scE_cell{ii},ccE_cell{ii},scC_cell{ii},...
        ccC_cell{ii}); %// write data
    end
    fclose(fid);
     


    Error statement:
    Index exceeds matrix dimensions.

    What am I doing wrong? Input would be greatly appreciated
     
    Last edited: Dec 18, 2014
  2. jcsd
  3. Dec 19, 2014 #2

    DrClaude

    User Avatar

    Staff: Mentor

    num2str(array.') doesn't create an array of strings, by a single string with all the elements of the array. Removing the .' should work. But I don't understand why you convert the numbers to strings before printing them.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Matlab - Index exceeds matrix dimensions when writing .txt
Loading...