Matlab does not give the right answer

답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 19일

0 개 추천

Check if your file filename_2 exist in your folder
Ilham Hardy
Ilham Hardy 2012년 11월 19일

0 개 추천

Seems for me it has more to do with
for run= 2:2
and
for run= 1:1

댓글 수: 1

Ilham Hardy
Ilham Hardy 2012년 11월 19일
편집: Ilham Hardy 2012년 11월 19일
Use the debugger and check what are the value of
run
and
ic
And what if you use only the command without any for loop.

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

Image Analyst
Image Analyst 2012년 11월 19일
편집: Image Analyst 2012년 11월 19일

0 개 추천

Not really sure - maybe it's a conflict between using the command line version of load and the function version (do you even know the difference?) Do you really have an equal sign in the filename, and no extension (such as .mat) for it, and that filename_2 is the actual filename and not the variable name? Anyway, assuming that's the case, try this more robust code:
for run = 2:2
for ic = 11:12
if ic == 11
fullFileName = fullfile(pwd, 'filename_2');
else
fullFileName = fullfile(pwd, 'filename_2_eps=1');
end
if exist(fullFileName, 'file')
load(fullFileName)
else
message = sprintf('Error: file not found:\n%s', fullFileName);
uiwait(warndlg(message));
end
end
end
Or maybe it's the fact that you're using run as a variable name but MATLAB considers it a built-in function name (so you should not use run as a variable name, just like you shouldn't use for, while, sprintf, load, etc. as variable names either).

댓글 수: 4

Phong Pham
Phong Pham 2012년 11월 19일
편집: Phong Pham 2012년 11월 19일
this is what i have on mine
inside of Filename has X0in, other files have X0inPert
i did change run to nr. still does not look right. Thanks for all your helps. I appreciate that . Thanks again.
for nr=2:2
for ic=11:12
if ic==11
nameS=strcat(pdir,'Filename/','Filename_',int2str(nr));
load(nameS);
X0in=X0in;
else
nameSi=strcat(pdir,'Filename/','Filename_',int2str(nr)','_eps=',int2str(1));
load(nameSi);
X0in=X0inPert;
end
Image Analyst
Image Analyst 2012년 11월 19일
편집: Image Analyst 2012년 11월 19일
Any reason why you chose to not include the parts of my code that make it robust (the call to fullfile() and the call to exist() and warndlg())? Also, I prefer sprintf() to strcat(), but it's your call. If you use my code, does it ever warn you that the file does not exist? If not, what's the problem now? I don't know what this means: "i still dont get the right answer for filename_2" Does that mean that filename_2 does not contain the correct filename, or that you are unable to read the contents of the file specified by filename_2? What format is filename_2 - Is it a text file, a csv file, a .mat file, or something else? Why doesn't it have an extension to indicate that? How was it created?
Image Analyst
Image Analyst 2012년 11월 19일
I don't know what the second sentence means. What does "not right" mean to you? Also, there were 7 questions in my prior comment and you answered only 2 of them, at most.
Walter Roberson
Walter Roberson 2012년 11월 26일
Your loop is overwriting X0in on each iteration, it appears.

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

카테고리

도움말 센터File Exchange에서 Variables에 대해 자세히 알아보기

태그

질문:

2012년 11월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by