Add features tosignal matrix

3 views (last 30 days)
Elzbieta
Elzbieta on 4 Jul 2024
Answered: Aditya on 4 Jul 2024
Hello,
With the following code I am concatenating signal matrices in mat files having in file names the following strings: *trial*ECG*data.
However I would like to include in the added columns the lost information like which are not included in the new file names:
  1. number of patient
  2. number of device
  3. external condition
Here is the following code:
names = {'Alessandra', 'Alfredo', 'Carla', 'Giulia'}
for i = 1: length(names)
filePattern = fullfile(myFolder,['*',names{i},'*trial*ECG*data.mat']) % Change to whatever pattern you need.
theFiles = dir(filePattern)
data = [];
for k = 1 : length(theFiles)
new_data = [];
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName)
display("new_data")
new_data = load(fullFileName, '-ascii');
size(new_data)
if (isrow(new_data) == 1)
data = [data; new_data'];
else
data = [data; new_data];
end
size(data)
data;
end
outFullFileName = fullfile(myFolder,[names{i},'_trial_ECG_data.txt'])
save(outFullFileName, 'data', '-tabs', '-ascii')
end

Answers (1)

Aditya
Aditya on 4 Jul 2024
Hi Elzbieta,
To include additional information such as the number of patients, number of devices, and external conditions in the concatenated signal matrix, you will need to modify the code to extract and append this information to the data matrix. Assuming that this information is available in the filenames or can be extracted from the files, here is an updated version of your code:
names = {'Alessandra', 'Alfredo', 'Carla', 'Giulia'};
for i = 1: length(names)
filePattern = fullfile(myFolder, ['*', names{i}, '*trial*ECG*data.mat']);
theFiles = dir(filePattern);
data = [];
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
new_data = load(fullFileName, '-ascii');
% Extract information from filename
tokens = regexp(baseFileName, 'patient(\d+)_device(\d+)_condition(\w+)_trial_ECG_data.mat', 'tokens');
if ~isempty(tokens)
patientNumber = str2double(tokens{1}{1});
deviceNumber = str2double(tokens{1}{2});
externalCondition = tokens{1}{3};
else
patientNumber = NaN;
deviceNumber = NaN;
externalCondition = 'Unknown';
end
% Convert condition to numeric
conditionMap = containers.Map({'conditionA', 'conditionB', 'conditionC'}, [1, 2, 3]);
conditionNumeric = conditionMap(externalCondition);
% Append information
numRows = size(new_data, 1);
additionalInfo = repmat([patientNumber, deviceNumber, conditionNumeric], numRows, 1);
new_data = [new_data, additionalInfo];
% Concatenate data
data = [data; new_data];
end
outFullFileName = fullfile(myFolder, [names{i}, '_trial_ECG_data.txt']);
save(outFullFileName, 'data', '-tabs', '-ascii');
end
Changes made:
  1. Extract Additional Information: Using regexp to extract the number of patients, number of devices, and external conditions from the filename.
  2. Convert External Condition to Numeric: Using a containers.Map to map external condition strings to numeric values.
  3. Append Additional Information: Adding the extracted information to each row of the data matrix before concatenation.
I hope this helps!

Community Treasure Hunt

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

Start Hunting!