PROBLEM IN sprintf

hi,
in the following ode why k become larger than L when use sprintf, where I got this error when run code:
%%%%%%%%%%%%%
Attempted to access b1(5123); index out of bounds because numel(b1)=5122.
Error in ==> webscop at 9
mat(i,j)=b1(k);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
the code:
f=fopen('ws1.txt','r+');%%%%%%open the original data file
b1=fscanf(f,'%d');
b=sprintf('%d\n', b1);
fclose all
L=length(b1);
k=1;i=1
while k <=L
mat(i,j)=b1(k);
k=k+1;
end
i=i+1;
end
thanks

댓글 수: 3

Image Analyst
Image Analyst 2011년 10월 11일
What exactly is "j"? And I see an "end" for the while (right before you increment i), but what is the other "end" for?
Jan
Jan 2011년 10월 11일
@huda: It is still unclear, what you want to achieve. I guess, a single REPMAT call solves your problem.
huda nawaf
huda nawaf 2011년 10월 11일
sorry , there is missing statement
this is the proper code:
f=fopen('ws1.txt','r+');%%%%%% open the original data file
b=fscanf(f,'%d');
b1=sprintf('%d\n', b);
fclose all
L=length(b1);
k=1;i=1
while k <=L
for j=1:4
mat(i,j)=b1(k);
k=k+1;
end
i=i+1;
end
thanks

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2011년 10월 11일

0 개 추천

Your "i=i+1" appears to be unneeded, in that it is not inside any loop and the end of your routine is immediately afterwards.
You appear to be using the subscript "j" for mat(i,j) but "j" is not defined in anything you show. As we do not know its value we cannot tell you what is going on. Worse yet we will assume that j has its default value of sqrt(-1) and that you are trying to index an impossible location.
Is "mat" completely uninitialized or is it passed in as part of the missing function header? And there must be a function header or else the "end" would be a syntactic error and you would not be able to run the routine at all.
Real code, please!

댓글 수: 5

Image Analyst
Image Analyst 2011년 10월 11일
I agree. It looks like this was posted immediately upon seeing the error without trying to figure it out himself at all. I think a coder should at least spend half an hour or so trying to fix it himself before tossing it out here for other people to fix. Situations like this should not be that tough to figure out. And this was his second post. His first one was even worse before he edited it.
Walter Roberson
Walter Roberson 2011년 10월 11일
I edited this one to line everything up so that the "end" matching would be obvious.
huda nawaf
huda nawaf 2011년 10월 11일
sorry , it was missing statement.I used this file ws1.txt (part of original file), the original file (ws.data) is very long and running time 20 minutes.
is this running time for such size (4MB) nutral?
f=fopen('ws1.txt','r+');%%%%%% open the original data file
b=fscanf(f,'%d');
b1=sprintf('%d\n', b);
fclose all
L=length(b1);
k=1;i=1
while k <=L
for j=1:4
mat(i,j)=b1(k);
k=k+1;
end
i=i+1;
end
Walter Roberson
Walter Roberson 2011년 10월 11일
Such long running times are pretty common when you do not preallocate the arrays.
Why not replace your inner loop with
mat(i,1:4) = b1(k:k+3);
k = k + 4;
And having done that, why not replace the entire computation with
mat = reshape(b1(:),4,[]).' ;
This should, of course, lead you to ask, "But what if b1 is not an exact multiple of 4 characters?", but since you do not check that in your original code, we must assume that you have external knowledge that this will never be the case.
I am somewhat mystified that you are storing the character representation of b rather than b itself, but I guess you have your reasons.
huda nawaf
huda nawaf 2011년 10월 12일
thank you very much,
I were not believing that my problem can be solved in instant.
I do appreciate that

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2011년 10월 11일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by