save a data in excel
Show older comments
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
Bob Thompson
on 15 Nov 2018
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.
Shel
on 15 Nov 2018
Bob Thompson
on 16 Nov 2018
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
on 21 Nov 2018
Bob Thompson
on 21 Nov 2018
What value does file1 contain?
Shel
on 21 Nov 2018
Answers (1)
Guillaume
on 21 Nov 2018
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
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!