Storing structure data into workspace?

My function reads certain .txt files and organizes the data inside of it into structures. I have a loop where it reads the files and shows the output in the command box. It saves the last .txt file structure as an ans in the work space. Which is what I want, but for every file. So all it does is read all of the files and only saves the last one. My end goal is to send this data into MySql, so I'm trying to figure out how to store all of the data into the work space, not just one. I know I have to add something to end of my for loop, but everything I try ends up in failure.
function [statusReports] = FileTesting(fileName, subjectKeyword, fromKeyword, dateKeyword, toKeyword, statusReportKeyword, activityKeyword, boatKeyword, siteKeyword)
subjectKeyword = 'Subject: ';
fromKeyword = 'From: ';
dateKeyword = 'Date: ';
toKeyword = 'To: ';
statusReportKeyword = 'BOEING FIELD ENGINEERING STATUS REPORT FOR ';
activityKeyword = 'ACTIVITY: ';
boatKeyword = 'SSBN ';
siteKeyword = 'SITE ';
x=1;
s = struct([]);
%Specify the folder where the files live.
myFolder = 'C:\Users\qzh14\Desktop\BAttle\Test';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.txt'); % Looks for text files in the folder
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fileName);
rawText = fileread(fileName);
%Find the index of all the special keyword data:
subjectIndices = strfind(rawText, subjectKeyword) + length(subjectKeyword);
fromIndices = strfind(rawText, fromKeyword) + length(fromKeyword);
dateIndices = strfind(rawText, dateKeyword) + length(dateKeyword);
toIndices = strfind(rawText, toKeyword) + length(toKeyword);
statusReportIndices = strfind(rawText, statusReportKeyword) + length(statusReportKeyword);
activityIndices = strfind(rawText, activityKeyword) + length(activityKeyword);
boatIndices = strfind(rawText, boatKeyword);
siteIndices = strfind(rawText, siteKeyword);
cogIndices = sort([boatIndices siteIndices]); %Combination of the boat and site report indices
% Loop over the number of emails within the file:
for i=1:length(subjectIndices)
statusReports(i).EmailSubject = GetTextToEndLine(rawText, subjectIndices(i));
statusReports(i).EmailFrom = GetTextToEndLine(rawText, fromIndices(i));
statusReports(i).EmailDate = GetTextToEndLine(rawText, dateIndices(i));
[statusReports(i).EmailTo endIndex] = GetTextToEndLine(rawText, toIndices(i));
statusReports(i).StatusReportDate = GetTextToEndLine(rawText, statusReportIndices(i));
if i < length(subjectIndices)
statusReports(i).EmailContents = strtrim(rawText(endIndex+1: subjectIndices(i)-length(subjectKeyword)-1));
else
statusReports(i).EmailContents = strtrim(rawText(endIndex+1: end));
end
statusReportCogIndices = GetIndexSubset(subjectIndices, i, cogIndices);
statusReportActivityIndices = GetIndexSubset(subjectIndices, i, activityIndices);
for j = 1:length(statusReportCogIndices)
[entryText endIndex] = GetTextToEndLine(rawText, statusReportCogIndices(j));
[entry.Activity endIndex] = GetTextToEndLine(rawText, statusReportActivityIndices(j));
[entry.Boat, entry.BoatName, entry.Missle, entry.Subsystem, entry.Unknown, entry.Location ] = ParseReportEntryLine(entryText);
if j < length(statusReportCogIndices)
entry.Status = strtrim(rawText(endIndex+1:statusReportCogIndices(j+1)-1));
elseif i < length(subjectIndices)
entry.Status = strtrim(rawText(endIndex+1:subjectIndices(i+1)-1));
else
entry.Status = strtrim(rawText(endIndex+1:end));
end
statusReports(i).Entries{j} = entry;
end
end
s=statusReports
end

 Accepted Answer

You should be avoiding storing data into another workspace. You should call the function and the routine doing the calling should store the result.

5 Comments

Thanks, I'll read up on these links and see what I can do first
So I read both the sections. Since I'm new with programming, I've never worked with GUIs. I understand your idea of making another script which calls this function, but I'm not sure how I would call something like this. I have like 4 other functions I called, but they are smaller. If you could help me understand the process.
*Also why is it bad to save it onto this workspace? Thanks!
Change
s=statusReports
to
s(k).filename = fileName;
s(k).reports = statusReports;
and change
function [statusReports] = ...
to
function [s] = ...
... and fix your indentation.
This will result in the output being a structure with as many entries as there are files, each entry storing the filename and all the reports related to that file.
If it does not matter to you for final output which report comes from which file, then instead:
s{k}= statusReports;
and then after the for k loop,
s = vertcat(s{k});
I edited the code you told me to and I see the file names and all reports in that file. Which is pretty awesome! I want to see what it looks like without the file names, but I don't understand s{k}= statusReports; and where it would go in the code.
Nvm I figured out that you just replace the bottom code and get rid of the filename structure. Thank you for all of your help!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!