Matlab Doesn't Recognize Fits Files (evalin, sprintf)?

조회 수: 4 (최근 30일)
MC
MC 2015년 11월 16일
편집: Stephen23 2019년 6월 19일
I need my script to read in five images (capture_667.fit... capture_671.fit), and name them image667... image 671. I have the following code:
for j = 667:671
evalin('base', sprintf('[ image%03d ] = fitsread(capture_%03d.fit);', j, j));
end
However, I get this error message:
.
'Undefined variable "capture_667" or class "capture_667.fit".
Error in Try_Sprintf_Evalin_New (line 5) evalin('base', sprintf('image%03d = fitsread(capture_%03d.fit);', j, j));'
.
All of the files I'm trying to read in are in the right folder. Furthermore, if I try to open these files manually, I get this message:
.
'Error using open (line 162) CFITSIO library error (412); datatype conversion overflow'
.
What's stopping my code from working? And what is a datatype overflow? I've looked online for a definition but with no luck.
NB: This task was set by demonstrators at university. I HAVE to complete it using sprintf, evalin, and fitsread.

채택된 답변

Thorsten
Thorsten 2015년 11월 16일
You forgot to put the image name into quotation marks to mark them as strings; note that you have to use two because they appear inside quotation marks:
evalin('base', sprintf('image%03d = fitsread(''capture_%03d.fit'');', j, j));
A better way would be without evalin
for j = 667:671
filename = sprintf('capture_%03d.fit', j);
I{j} = fitsread(filename);
end
You can then work with I{667}, for example. One drawback of the evalin approach is that it is more error prone (you've just made the experience).
  댓글 수: 1
MC
MC 2015년 11월 16일
It works! Thank you so much! I wasn't aware of the double-' part: I'd tried with single and when I noticed that it "ended" the string, I thought I couldn't use any.

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2015년 11월 16일
If you need your code to do that, then it is time to renegotiate the requirements with who-ever imposed that constraint. Creating variables on the fly is not recommended at all.
  댓글 수: 3
Walter Roberson
Walter Roberson 2015년 11월 16일
Well now you begin to see why we do not recommend using eval() or evalin() .
Is the requirement definitely for evalin()? You cannot use assignin() instead?
MC
MC 2015년 11월 16일
Definitely evalin. This task also requires me to use sprintf and fitsread.

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


Stephen23
Stephen23 2015년 11월 16일
편집: Stephen23 2019년 6월 19일

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by