save a data in excel

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 Comments

As you currently have it set up, xlswrite will overwrite the same cells with the new data during each iteration of the for loop. I would suggest putting the information you want to save into indexed rows of an array, and then printing the entire array to excel after the loop has completed.
Thanks,
well I have changed it to the following
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';
i
area=pixel*npixel0
ni=2
%out=zeros(totalfile,ni)
out(i,1)=i
out(i,2)=area
xlswrite('result.xlsx',out)
fclose(H);
end
but got this error
Error using xlswrite (line 226)
Invoke Error, Dispatch Exception:
Source: Microsoft Excel
Description: Excel cannot open the file 'result.xlsx' because the file format or file extension is not valid.
Verify that the file has not been corrupted and that the file extension matches the format of the file.
Help File: xlmain11.chm
Help Context ID: 0
I really do not know what the problem is with opening excel so I changed to save it as a text file as following
H='result.txt';
file1=fopen(H);
area=pixel*npixel0;
header1=i;
header2=area;
fprintf(file1,[header1,header2 ]);
fprintf(file1,'%d %d',[i,area]);
fclose(file1);
but again got the error:
Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.
Does the file exist? I usually find that I'm getting those types of errors when Matlab is having trouble generating the file, or it doesn't exist for some reason. Also, double check that you don't have the file open, as that can mess with Matlab's ability to open it.
Shel
Shel on 21 Nov 2018
Hi,
yes it exists in the same folder of the images and it is not open (none of them ... excel ot text files)!
What value does file1 contain?
Shel
Shel on 21 Nov 2018
non of them (.txt or .xlsx) contains any value after running ... the error just come up

Sign in to comment.

Answers (1)

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

Asked:

on 15 Nov 2018

Commented:

on 26 Nov 2018

Community Treasure Hunt

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

Start Hunting!