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

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.

 Akzeptierte Antwort

Thorsten
Thorsten am 16 Nov. 2015
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 Kommentar

MC
MC am 16 Nov. 2015
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Walter Roberson
Walter Roberson am 16 Nov. 2015

0 Stimmen

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 Kommentare

MC
MC am 16 Nov. 2015
It's part of a lab for uni, so I have to do this using sprintf, fitsread, and evalin. If the files are to be read in, I need to assign them names for when they enter the workspace.
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 am 16 Nov. 2015
Definitely evalin. This task also requires me to use sprintf and fitsread.

Melden Sie sich an, um zu kommentieren.

Gefragt:

MC
am 16 Nov. 2015

Bearbeitet:

am 19 Jun. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by