필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Help with writing results of a code sequence to a text file

조회 수: 1 (최근 30일)
Ellis Berry
Ellis Berry 2016년 3월 22일
마감: Walter Roberson 2016년 3월 23일
Hi everyone, I have some code that processes images and will produce some results such as a string saying whether or not the experiment is complete, the number of black pixels and the time of the picture. At the moment, I use 'disp' so they appear in my matlab command window, but I want these results to be written to a text file as they are produced. Can anyone help me do this? With commands such as 'fopen' etc? Please explain in dummy terms as I am fairly new to Matlab! Here is my code so far: Thankyou in advance.
% --- Executes on button press in pushbutton3. %MAIN!!
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%The picture interval is entered in editbox2, if not, this error message
%appears.
Interval=str2num(char(get(handles.edit2,'String'))); %Prompt for user to enter camera interval.
if isempty(Interval)
errordlg('Error, please load pictures to be processed and enter a picture time interval before clicking Run.');
else
%In and out directories chosen by pushbutton1 and pushbutton2
outDir = handles.outDir;
inDir = handles.inDir;
includeSubdirectories = true;
% All extensions that can be read by IMREAD
imreadFormats = imformats;
supportedExtensions = [imreadFormats.ext];
% Add dicom extensions
supportedExtensions{end+1} = 'dcm';
supportedExtensions{end+1} = 'ima';
supportedExtensions = strcat('.',supportedExtensions);
% Allow the 'no extension' specification of DICOM
supportedExtensions{end+1} = '';
% Create a image data store that can read all these files
imds = datastore(inDir,...
'IncludeSubfolders', includeSubdirectories,...
'Type','image',...
'FileExtensions',supportedExtensions);
h=waitbar(0, 'Please Wait...');
% Process each image using trial_3 (Image Processing toolbox app that let
% me set the HSV thresholds).
for imgInd = 1:numel(imds.Files)
perc=numel(imds.Files);
inImageFile = imds.Files{imgInd};
% Output has the same sub-directory structure and file extension as
% input
outImageFile = strrep(inImageFile, inDir, outDir);
try
% Read
im = imds.readimage(imgInd);
% Process
im = trial_3(im);
% Write
if(isdicom(inImageFile))
dicommeta = dicominfo(inImageFile);
dicomwrite(im, outImageFile, dicommeta, 'CreateMode', 'copy');
else
imwrite(im, outImageFile);
end
disp(['PASSED:', inImageFile]);
catch allExceptions
disp(['FAILED:', inImageFile]);
disp(getReport(allExceptions,'basic'));
end
waitbar(imgInd/perc, h);
drawnow;
end
delete(h);
%Specify the folder where the files (Pictures) live. Chosen by pushbutton2
myFolder=handles.outDir;
%Get a list of all files in the folder with the desired file name pattern.
filePattern=fullfile(myFolder, '*.JPG');
theFiles=dir(filePattern);
caListBoxItems = cell(length(theFiles), 1);
for k=1:length(theFiles)
baseFileName=theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
thisString = fprintf(1, 'Now reading %s', fullFileName);
fprintf('%s\n', thisString);
caListBoxItems{k} = thisString;
OutputFileNames{k} = theFiles(k).name;
set(handles.listbox2, 'String', OutputFileNames); %listbox2 will display file names of processed images.
drawnow; % Force immediate screen repaint.
image=imread(fullFileName);
white=nnz(image);
black=(numel(image)-white);
if black>(numel(image)*0.0193); %if black pixels is more than 1.93% of pixels in image, experiment complete. Value can be altered.
disp('The experiment is complete at this stage!')
fprintf('The number of Black Pixels is:');
disp(numel(image)-white);
disp('Time of Picture (Secs): ');
disp((k-1)*Interval); %Here, "Interval" is a variable chosen by user (15 secs, 30 secs etc)
else
disp('The experiment is not complete yet.')
fprintf('The number of Black Pixels is:');
disp(numel(image)-white);
end
guidata(hObject,handles);
end
end
  댓글 수: 2
dpb
dpb 2016년 3월 22일
You've pretty much described the process...open an output file with fopen, saving the returned file handle. Use fprintf with that file handle instead of either 1 or implicit as seem to have done and replace disp with fprintf as well. Note you'll need to
  1. Use a suitable format string for numeric values, and
  2. Remember to write the newline '' to advance to next line
Also, don't forget to fclose(fid) the file before the script/function exits.

답변 (0개)

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by