Error using fprintf. Function is not defined for 'struct' inputs.

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 comentarios

Your answer is on line 3 of the first block of your own supplied code.
prahlad h
prahlad h el 22 de Mzo. de 2017
Editada: prahlad h el 22 de Mzo. de 2017
Could you please elaborate? I know dicomFiles is a Nx1 struct array with fields:
name
date
bytes
isdir
datenum
The only thing I want to write into the file, is the name, of each file, which is why maybe dicomFiles(k, 1) would work, but it doesn't.
Also, xclass seems to be a struct too.
dicomFiles(k).name
As Greg told you, this already is used on line three.
Goodness..what a dumb moment. I wasted hours on this haha. Thanks so much.

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 22 de Mzo. de 2017
Editada: Jan el 22 de Mzo. de 2017
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)

1 comentario

I was writing each time the loop was running. That wasn't a problem. Thanks for the answer though, it did work, stupidity on my part.
fprintf(fid3, '%s %s \n', dicomFiles(k).name, xclass);
^wouldn't work now because apparently xclass is a cell - Error using fprintf Function is not defined for 'cell' inputs.
If you have an answer for this, then great. Else, I'd just compare the value of xclass to the groups and write the output to 2 separate files.
Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.

Preguntada:

el 22 de Mzo. de 2017

Comentada:

el 28 de Dic. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by