Matlab - Index exceeds matrix dimensions when writing .txt

Click For Summary
SUMMARY

The discussion addresses a common error in MATLAB: "Index exceeds matrix dimensions" when attempting to write data from a matrix and cell array to a text file. The user is working with a 27x5 matrix named uData and a 27x1 cell array called name_Database. The issue arises from improper indexing and conversion of numeric arrays to strings using num2str(array.'), which concatenates elements into a single string. The solution involves removing the transpose operator (.) to correctly convert each element to a string.

PREREQUISITES
  • Familiarity with MATLAB programming language
  • Understanding of matrix and cell array structures in MATLAB
  • Knowledge of file I/O operations in MATLAB
  • Experience with the fprintf function for formatted output
NEXT STEPS
  • Review MATLAB documentation on matrix indexing and dimensions
  • Learn about MATLAB cell arrays and their manipulation
  • Explore file writing techniques in MATLAB, specifically using fopen and fprintf
  • Investigate string conversion functions in MATLAB, particularly num2str
USEFUL FOR

This discussion is beneficial for MATLAB users, particularly students and professionals dealing with data output and file management in their programming tasks.

muaaman
Messages
4
Reaction score
0

Homework Statement


Index exceeds matrix dimensions

Homework Equations

The Attempt at a Solution


I am trying to output into a text file something like this:
Code:
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:
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:
Physics news on Phys.org
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
10K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K