Matlab - Index exceeds matrix dimensions when writing .txt

In summary, the conversation is about an error stating that the index exceeds the dimensions of a matrix. The attempt at a solution involves outputting data into a text file, but there is an error due to converting numbers to strings before printing them.
  • #1
muaaman
5
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
  • #2
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.
 

FAQ: Matlab - Index exceeds matrix dimensions when writing .txt

1. Why am I getting an "Index exceeds matrix dimensions" error when trying to write a .txt file in Matlab?

The "Index exceeds matrix dimensions" error occurs when you are trying to access an element in a matrix that does not exist. This can happen when your matrix is not properly initialized or when you are using incorrect indices.

2. How can I fix the "Index exceeds matrix dimensions" error in Matlab?

To fix this error, you will need to carefully check your code and make sure that your matrix is properly initialized and that you are using correct indices to access elements. You may also need to use the size() function to check the dimensions of your matrix before accessing elements.

3. Can using incorrect indices cause the "Index exceeds matrix dimensions" error in Matlab?

Yes, using incorrect indices is one of the most common causes of this error in Matlab. Make sure that you are using valid indices within the dimensions of your matrix.

4. Is there a way to prevent the "Index exceeds matrix dimensions" error in Matlab?

To prevent this error, it is important to carefully plan and test your code before running it. Make sure that your matrix is properly initialized and that you are using valid indices to access elements. You can also use the size() function to check the dimensions of your matrix before accessing elements.

5. Are there any other possible causes of the "Index exceeds matrix dimensions" error in Matlab?

Yes, there may be other causes of this error such as using incorrect functions or attempting to access elements in a multidimensional array. It is important to carefully review your code and check for any potential errors.

Similar threads

Replies
7
Views
7K
Replies
1
Views
5K
Replies
1
Views
3K
Replies
5
Views
9K
Replies
1
Views
3K
Replies
1
Views
3K

Back
Top