How add field to a import structure file *.txt.?
3 次查看(过去 30 天)
显示 更早的评论
Hello fellows I have a problem I need to add a field to a import text file.
I have some field's in a .txt file i need to use , but I need to add a new fields to structure, can someone help me with this?
Thanks in advance
fileID = fopen(fullfile( FilePath, FileName),"r");
%RELOAD FILE DATA IN MATRIX FORMAT WITH ALL COLUMNS
dataAcquisition = readmatrix(fullfile(FilePath,FileName),'NumHeaderLines',headerLine);
%CHOOSE THE COLUMN's WITH THE DATA NEEDED
ixKeep=[1 2:7]
%LOAD ALL REQUIRED FIELDS
dataAcquisition = dataAcquisition(:,ixKeep);
%%%%%%%%%%%%%ADD STRUCTURE WITH A FOLLOWING FIELDS%%%%%%%%%%%%%%
%SAMPLE | TIMESAMPLE | ECG | RESPIRATORY | TEMPERATURE | BATTERY
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
validFields = {'sample', 'time_aquisition', 'ecg', 'respiratory', 'temperature', 'baterry'}
2 个评论
Chunru
2022-9-19
You are using readmatrix to get a matrix (rather than a structure). You can add in new culumn to the matrix but not field.
回答(1 个)
Harsh
2025-6-25
You are attempting to read a ".txt" file using "readmatrix" with a fixed number of header lines ('NumHeaderLines', headerLine). However, this method assumes the header is always the same length, which is unreliableas the "input.txt" file uses a dynamic or custom header ending with a marker like "# EndOfHeader".
The "fgetl" function must be used in a loop to dynamically skip lines until it encounters "# EndOfHeader". Once the header is bypassed, "textscan" is required to read the numeric data, providing precise control over column formatting. The resulting cell array must be converted to a matrix using "cell2mat", after which the relevant columns are to be extracted. A "sample" vector is constructed using simple indexing, and a "time_acquisition" vector is computed based on a known sampling rate (e.g., 125 Hz). Finally, the data must be organized into a "table" with clearly defined field names for structured analysis.
Below is sample code to perform this-
fullFileName = "path to input.txt";
% Open file and skip header until '# EndOfHeader'
fid = fopen(fullFileName, 'r');
line = fgetl(fid);
while ischar(line) && ~contains(line, '# EndOfHeader')
line = fgetl(fid);
end
% Read numeric data after header
rawData = textscan(fid, repmat('%f', 1, 11), 'Delimiter', '\t');
fclose(fid);
% Convert to matrix
dataMatrix = cell2mat(rawData);
% Extract relevant columns (e.g., ECG, Respiratory, Temperature, Battery)
ixKeep = [4, 5, 6, 7]; % Adjust if needed
dataAcquisition = dataMatrix(:, ixKeep);
% Add sample counter and time vector
numSamples = size(dataAcquisition, 1);
sample = (1:numSamples)';
fs = 125; % Sampling frequency in Hz
time_acquisition = (0:numSamples-1)' / fs;
% Create structured table
structuredData = table(sample, time_acquisition, ...
dataAcquisition(:,1), dataAcquisition(:,2), ...
dataAcquisition(:,3), dataAcquisition(:,4), ...
'VariableNames', {'sample', 'time_acquisition', 'ecg', 'respiratory', 'temperature', 'battery'});
Here's a snapshot of the "structuredData"-

Hope this resolves your query!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!