[SOLVED] Matlab error :??? Index exceeds matrix dimensions.

Status
Not open for further replies.

blooz

Advanced Member level 2
Joined
Dec 29, 2010
Messages
560
Helped
121
Reputation
242
Reaction score
116
Trophy points
1,343
Location
India
Activity points
4,985
hi the particular code

is


%%%%%%%%%
r=randint(1,d,range);
temp=dec2hex(r',(w/4))

for i=1:d-1;
fid(fid,'%d,\n',temp(1:2 ,i));//error reported here
end


error message

??? Index exceeds matrix dimensions.

Error in ==> coe_script at 19
fid(fid,'%d,\n',temp(1:2 ,i));
 

the initial matrix r is single dimension array which has length on x = range and on y = 1.

the temp is transpositioned (because you use r') compared to r so it has length on x = 1 and on y = range.

Trying to access element x=2 and with some Y on temp will rise error.

But fid(fid,'%d,\n',temp(1 ,i)); should be OK
 


the full program is

////////////////////////////////////////////////////////////////////////////////////

%Script to generate a .coe file for memory initialisation
clc
fid=fopen('mem.coe','wt');
x=0;
fprintf(fid,' %b',x);
d=input('Please enter depth of memory ');
w=input('Please enter the width of memory ');
range=power(2,w);
r=randint(1,d,range);
temp=dec2hex(r',(w/4));
fprintf(fid,';The data memory generated is ');
fprintf(fid,'MEMORY_INITIALIZATION_RADIX=16; ');
fprintf(fid,'MEMORY_INITIALIZATION_VECTOR= ');
d=d-2;
display('temporary-Assignment')
size(temp)
temp
for i=1:d-1;
fid(fid,'%d,\n',temp(1,i));
end
fprintf(fid,';');
fclose(fid);
///////////////////////////////////////////////

when run the ouput is


Please enter depth of memory 10
Please enter the width of memory 8
temporary-Assignment

ans =

10 2


temp =

1E
73
B7
E4
45
41
DD
3B
CE
E8

??? Index exceeds matrix dimensions.

Error in ==> coe_script at 19
fid(fid,'%d,\n',temp(1,i));
 

what is the "fid" function?
Code:
for i=1:d-1;
fid(fid,'%d,\n',temp(1,i));
end
You might want to use "fprintf"
Moreover, you should be saving "temp(1," instead of "temp(1,i)", using "%x" instead of "%d".
 
Reactions: blooz

    blooz

    Points: 2
    Helpful Answer Positive Rating
Perfect fix and the program is working ....

---------- Post added at 23:01 ---------- Previous post was at 22:49 ----------

The program is producing correct output matlab command Window but in the file produced the output is in decimal format and the number of elements is more than expected how to fix the issue


Code:
//command window output 

Please enter depth of memory 12
Please enter the width of memory 8
temporary-Assignment

ans =

    12     2


temp =

2C
FA
45
40
E0
BC
22
03
E4
32
4C
A9

Code:
///file output 
;The data memory generated is 
 MEMORY_INITIALIZATION_RADIX=16;
 MEMORY_INITIALIZATION_VECTOR= 50,
67,
70,
65,
52,
53,
52,
48,
69,
48,
66,
67,
50,
50,
48,
51,
69,
52,
;
 

Ooops, my mistake, you should use "%s" in the output formatting.
 
Reactions: blooz

    blooz

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…