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));