What's wrong with this code?

Every time I run this code an error message appears saying '??? undefined function or method 'img' for input arguments of type 'char'.' I am attempting to view a coded image. What is the problem? Thanks,
clear all; clc;
[FileName,path] = uigetfile({'*.img'},'Choose ASL Label File');
k = length(FileName);
file = FileName(1:(k-4));
filenameh=strcat(path,FileName(1:(k-3)),'hdr');
fidh = fopen(filenameh,'r','b');
fseek(fidh,40,'bof');
fseek(fidh,40,'bof');
dim = fread(fidh,8,'int16');
hdrsize = fread(fidh,1,'int32','l');
if hdrsize == 348
datatype = 'l';
else
datatype = 'b';
end
data_format=fread(fidh,8,'int16',datatype);
if data_format(6)==4
form = 'short';
elseif data_format(6) ==16
form = 'float';
else
disp('Problem Reading In File')
quit cancel
end
xdim = dim(2);ydim = dim(3);zdim = dim(4);vols = dim(5);
disp('Analysing ....')
lab1f = strcat(path,file,'.img');
l1fid=fopen(lab1f,'r',datatype);
lab1=fread(l1fid,form);
lab1=reshape(lab1,xdim,ydim,zdim,vols);

2 个评论

Jack - if including code in your question (instead of attaching it using the paper clip button) please ensure that it is formatted correctly to improve readability. Highlight the code portions of the question and press the {} Code button.
The undefined function or method message typically means that the code is using something as a function which is not defined or found in the MATLAB search path, or it is invalid for inputs of a certain type. Your error message is for something called img which I don't see anywhere in your code except as a file extension.
Have you included all the code relevant to this question? What is the line number that has generated this error?
Please include the entire error message - ALL THE RED TEXT - don't snip or paraphrase it at all. By leaving out vital information (so far), you've prevented anyone from pinpointing and fixing your error.

请先登录,再进行评论。

 采纳的回答

Star Strider
Star Strider 2014-7-5

0 个投票

I’m not aware that ‘.img’ is a valid format in MATLAB. (I can’t find any reference to it, anyway.) You need to use the iminfo function to find out what its format is, and then read it with imread. You can then extract the data you need from the array created by imread. There are many other functions in the Image Processing Toolbox if you have access to it.

5 个评论

It's some sort of raw/proprietary format. That's why Jack's using fread() to read it in. He's writing his own custom file reader, though it should be its own function, not combined with a function that asks the user to select a file and analyze it also. And he should also NOT be using path as the name for one of his variables - VERY BAD! - because it will blow away an important built-in reserved function.
Thanks for clarifying that. I didn’t immediately pick up on the overshadowing of ‘path’ either.
I also don’t see ‘img’ used as a function. Apparently that line is over the horizon somewhere, unless it refers to the uigetfile statement?
You and Geoff are correct there. The code that references img has not been included in his post, for some weird reason.
It's been resolved, sorry for the confusion but its a very old script. Issue was with file name, it included spaces. Thanks for all your help.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by