save a data in excel

조회 수: 3 (최근 30일)
Shel
Shel 2018년 11월 15일
댓글: Shel 2018년 11월 26일
Hello everyone,
I am doing the segmentation of images and I need to save the number of image and the area of binarized image in an excel file, but I cannot understand what is the problem with my code. Can you please guide me how I can save my output in two columns?
Thanks
pixel=0.2*0.2;
totalfile=length(d);
for i=1:totalfile
fname=['1-',num2str(i), '.dcm'];
indicom=dicomread(fullfile(source_dir, fname));
otsu=graythresh(indicom);
BW_org=imbinarize(indicom,otsu);
BasedFileName=sprintf('1-%d.dcm',i);
FilteredFileName=fullfile(resultfolder1,BasedFileName);
dicomwrite(BW_org, FilteredFileName);
npixel0=sum(sum(BW_org==0));
H='result.xlsx';
file1=fopen(H');
i
area=pixel*npixel0
xlswrite('result.xlsx',i,area)
fclose(fid);
end
  댓글 수: 6
Bob Thompson
Bob Thompson 2018년 11월 21일
What value does file1 contain?
Shel
Shel 2018년 11월 21일
non of them (.txt or .xlsx) contains any value after running ... the error just come up

댓글을 달려면 로그인하십시오.

답변 (1개)

Guillaume
Guillaume 2018년 11월 21일
There are several puzzling things in the code you've written, which makes me think you don't fully understand what you're doing.
First,
fname=['1-',num2str(i), '.dcm'];
...
BasedFileName=sprintf('1-%d.dcm',i);
These two lines produce exactly the same result, using different methods. Get rid of one of them, my personal preference is to use sprintf as it's more readable.
Second, and the cause of your problem:
H='result.xlsx';
file1=fopen(H');
%...
xlswrite('result.xlsx',i,area)
You're mixing up two completely different types of file IO here. fopen open a file for writing, and writing is done with fprintf or fwrite. This is completely incompatible with xlswrite which wants the open the file itself. In fact, since you've already opened the file yourself (and not closed it), xlswrite will rightfully complain that it cannot open the file (a file can only be opened once for writing). Getting rid of that fopen would solve your problem.
Later on you say you've tried:
H='result.txt';
file1=fopen(H);
%...
fprintf(file1,[header1,header2 ]);
fprintf(file1,'%d %d',[i,area]);
fclose(file1);
which is the proper way of writing files with fopen. The typical reason for getting the error Invalid file identifier. Use fopen to generate a valid file identifier. would be because fopen failed to open the file for writing. And the most likely reason why it failed to do so is because the file that you opened in your first failed attempt is still open. To force matlab to close all files that it has opened:
fclose('-all') %close all opened file
Then your code above should work. I would still recommend modifying it to:
H='result.txt';
file1=fopen(H);
assert(file1 > 1, 'Failed to open result.txt. Is the file already open?')
cleanupobj = onCleanup(@() fclose(file1)); %ensure that file is closed even if an error occurs
%your calcs here
fprintf(file1,[header1,header2 ]);
fprintf(file1,'%d %d',[i,area]);
clear(cleanupobj) %close the file
  댓글 수: 1
Shel
Shel 2018년 11월 26일
Thank you very much.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by