Error using fprintf. Function is not defined for 'struct' inputs.
조회 수: 21 (최근 30일)
이전 댓글 표시
I'm reading all the DICOM files in a directory. I'm supposed to run 1 file after the other for K-NN classification. So, I run a loop, and individually classify the files for which I've used the for loop.
dicomFiles = dir('*.dcm');
for k = 1:(numfiles)
I=dicomread(strcat(location, '\', dicomFiles(k).name));
This is where I've called knnclassify() and when I use disp(xclass), it results in either of the groups i.e, 'Yes' or 'No'.
xclass = knnclassify(SM, B, G, VK, METH, RULE, KF);
I'm supposed to write the result of the classification into a file with the format -
Name Group
C1.dcm Yes
N1.dcm No
So, I've used the following code to write the contents into a file.
%Output KNN results to a file
op_dir = 'F:\8th sem Project\Code\KNN Result';
if exist(strcat(op_dir,'\KNN_Result.dat'),'file')
delete(strcat(op_dir,'\KNN_Result.dat'));
end
op = strcat(op_dir,'\KNN_Result.dat');
fid3 = fopen(op, 'a+');
fprintf(fid3, '%s %s \n', dicomFiles(k), xclass);
However, I get an error saying - Error using fprintf. Function is not defined for 'struct' inputs. I've tried going through all the posts on the internet regarding the same, but if I change it to dicomFiles{k}, there are different errors. I'm not used to with the syntax of Matlab. Please help!
댓글 수: 5
채택된 답변
Jan
2017년 3월 22일
편집: Jan
2017년 3월 22일
At first the code can be simplified:
%Output KNN results to a file -
% !!! open it ONCE BEFORE the loop over k!!!
file = fullfile('F:\8th sem Project\Code\KNN Result\KNN_Result.dat';
fid3 = fopen(op, 'w'); % Deletes formerly existing file automatically
if fid3 == -1, error('Cannot open file for writing: %s', file); end
Now the actual problem: The message means, that one of the arguments is a struct. Here it is at least "dicomFiles", which has been created by dir(). If the output should be the name (you did not explain this):
fprintf(fid3, '%s %s \n', dicomFiles(k).name, xclass);
Now I'm not sure, what the class of xlcass is. If it is really a string, the shown code will work.
Don't forget to close the file after the loop:
fclose(fid3)
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!