Storing structure data into workspace?

1 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2016-10-13
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 个评论
Sancheet Hoque
Sancheet Hoque 2016-10-13
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.
Sancheet Hoque
Sancheet Hoque 2016-10-13
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by