フィルターのクリア

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

24 ビュー (過去 30 日間)
prahlad h
prahlad h 2017 年 3 月 22 日
コメント済み: Walter Roberson 2017 年 12 月 28 日
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 件のコメント
prahlad h
prahlad h 2017 年 3 月 22 日
Goodness..what a dumb moment. I wasted hours on this haha. Thanks so much.

サインインしてコメントする。

採用された回答

Jan
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)
  1 件のコメント
prahlad h
prahlad h 2017 年 3 月 22 日
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!

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by