save a data in excel
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Guillaume
el 21 de Nov. de 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
Ver también
Categorías
Más información sobre File Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!