Storing structure data into workspace?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Sancheet Hoque
il 13 Ott 2016
Commentato: Sancheet Hoque
il 13 Ott 2016
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
0 Commenti
Risposta accettata
Walter Roberson
il 13 Ott 2016
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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su File Operations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!