creating structure based on txt file

3 visualizaciones (últimos 30 días)
Elzbieta
Elzbieta el 3 de Nov. de 2024
Respondida: Umar el 3 de Nov. de 2024
Hello,
I am trying to create a structure based on comments in txt files in such shape.
Record #: 92
Notes :
jola
2000
siedzenie
2024-07-3121:37:08Record #: 92
Notes :
jola
2000
siedzenie
However I am receiving struct onlhy for one person.
This is my attempt:
subject = {'gosia', 'kasia', 'jola', 'Kuba', 'robert', 'Tadeusz', 'Beata', 'Anna', 'pawel', 'piotr' };
condition = {'siedzaca', 'siedzenie', 'chodzenie', 'bieganie', 'truchtanie', 'dreptanie'};
freqs = {'freq1000', 'freq2000'};
%------get all measurement files
raw_file_path = 'C:\Users\Ela\Documents\MATLAB\database\measurements\raw\'
%file = 'Device_21_Volts.txt';
% ----- get measurements files
filePattern = fullfile(raw_file_path,['Device*Volts.txt']) % Change to whatever pattern you need.
theFiles = dir(filePattern)
for kFile = 1 : length(theFiles)
baseFileName = theFiles(kFile).name
fullFileName = fullfile(raw_file_path, baseFileName)
%------extract ecg signal
rawData = readmatrix(baseFileName,'DecimalSeparator',',');
disp(rawData(1,:)); % display row 1 of the data
% Create a structure to hold the ECG data
ecgData = struct();
ecg = struct();
% Therefore
ecgData.leadI=rawData(:,2);
ecgData.leadII=rawData(:,3);
% The augmented vector leads and lead III are derived as
% III=II-I; aVR=-(I+II)/2; aVL=I-II/2; aVF=II-I/2
ecgData.leadIII=ecgData.leadII-ecgData.leadI;
ecgData.aVR=-(ecgData.leadI+ecgData.leadII)/2;
ecgData.aVL=ecgData.leadII/2;
ecgData.aVF=ecgData.leadII-ecgData.leadI/2;
ecgData.V1 = rawData(:,8);
ecgData.V2 = rawData(:,4);
ecgData.V5 = rawData(:,7);
s = struct;
%------get measurement
file = fullFileName
field=textread(file,'%s','delimiter','\n')
%-------create empty struct
%-------for each subject
for isubject = 1:length(subject)
disp('found or not?')
subject{isubject}
find(~cellfun(@isempty,strfind(field,'piotr'))==true)
if (find(~cellfun(@isempty,strfind(field,subject{isubject}))==true))
for icond = 1:length(condition)
if (find(~cellfun(@isempty,strfind(field,condition{icond}))==true))
if (strcmp(condition{icond},'siedzaca') || strcmp(condition{icond},'siedzenie'))
cond = 'sitting';
elseif (strcmp(condition{icond},'chodzenie') || strcmp(condition{icond},'bieganie') || strcmp(condition{icond},'truchtanie') || strcmp(condition{icond},'dreptanie'))
cond = 'walking';
else
cond = 'sitting';
end
%-----add sampling frequency to structure
if (find(~cellfun(@isempty,strfind(field,freqs{2}))==true))
s.(subject{isubject}).(cond).(freqs{2}) = ecgData();
else
s.(subject{isubject}).(cond).(freqs{1}) = ecgData();
end
else
cond = 'sitting';
if (find(~cellfun(@isempty,strfind(field,freqs{2}))==true))
s.(subject{isubject}).(cond).(freqs{2}) = ecgData();
else
s.(subject{isubject}).(cond).(freqs{1}) = ecgData();
end
end
end
else
subject{isubject}
file
disp('subject subject{isubject} not found');
continue;
end
end
end

Respuestas (1)

Umar
Umar el 3 de Nov. de 2024

Hi @Elzbieta,

The issue you are encountering likely stems from the way you are checking for the presence of subjects and conditions in your text files. In your current implementation, you are using

find(~cellfun(@isempty,strfind(field,subject{isubject}))==true) 

to check for the existence of a subject. If the subject is not found, the loop continues without adding any data to the structure for that subject. To ensure that you capture data for all subjects, consider the following adjustments.

Check for All Subjects: Ensure that your condition checks are comprehensive and correctly structured. You may want to use a more robust method to verify if a subject exists in the file.

Initialize the Structure: Make sure that the structure s is initialized outside the loop and that you are correctly appending data for each subject and condition.

Debugging: Add debugging statements to track which subjects and conditions are being processed. This will help you identify where the logic may be failing.

Here is a refined snippet of your code to illustrate these points:

% Initialize the structure outside the loop
s = struct();
for isubject = 1:length(subject)
  subject_found = any(~cellfun(@isempty, strfind(field, subject{isubject})));
  if subject_found
      for icond = 1:length(condition)
          condition_found = any(~cellfun(@isempty, strfind(field, 
          condition{icond})));
          if condition_found
              % Determine condition type
              cond = determineCondition(condition{icond});
              % Add data to structure
              if any(~cellfun(@isempty, strfind(field, freqs{2})))
                  s.(subject{isubject}).(cond).(freqs{2}) = ecgData;
              else
                  s.(subject{isubject}).(cond).(freqs{1}) = ecgData;
              end
          end
      end
  else
      disp(['Subject ' subject{isubject} ' not found in file.']);
  end
end
function cond = determineCondition(condition)
  switch condition
      case {'siedzaca', 'siedzenie'}
          cond = 'sitting';
      case {'chodzenie', 'bieganie', 'truchtanie', 'dreptanie'}
          cond = 'walking';
      otherwise
          cond = 'unknown';
  end
end

This approach should help you capture data for all subjects and conditions as intended.

Hope this helps.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by