Obtain information after text pattern
4 views (last 30 days)
Show older comments
Richard Wood
on 13 Aug 2024
Answered: Walter Roberson
on 13 Aug 2024
Dear all
I am dealing with files of extension .mat (not related to MatLab) like the one attached. I would like to be able to save in a variable the numerical value that appears after the text pattern "material[x]:damping-constant = 1.0" (so, in this case, 1). As you can see, here x can take values from 1 to 8, so it would be useful for me to have an array where each index position stores the the corresponding value.
Any ideas?
0 Comments
Accepted Answer
Walter Roberson
on 13 Aug 2024
S = fileread('CrPS4.mat');
parts = regexp(S, '(?<=damping-constant\s+=\s+)\S+', 'match');
parts_numeric = str2double(parts)
0 Comments
More Answers (2)
nick
on 13 Aug 2024
Hi Richard,
You can extract the numerical value in all such text patterns "material[x]:damping-constant = 1.0" in the shared .MAT file by followiing these steps:
- Open the File : Use 'fopen' functionto open the file for binary read access.
- Read the Data : Use 'fread' function to read the data from the opened file.
- Extract the Numeric Value : You can then use the function 'regexp' with the output option set to 'token' to obtain the text of each captured token within the data in the file. Tokens are portions of the matched text that correspond to portions of the pattern.
- Store the Data : The data read then can be processed to be stored in the array.
Here is an example of how you can achieve it :
% Define the filename
filename = 'CrPS4.mat';
% Read the file content as a string
fileID = fopen(filename, 'r');
fileContent = fread(fileID, '*char')';
fclose(fileID);
% Initialize an array to store the damping constants
dampingConstants = zeros(1, 8);
% Define the regular expression pattern
pattern = 'material\[(\d+)\]:damping-constant = ([\d\.]+)';
% Find all matches in the file content
matches = regexp(fileContent, pattern, 'tokens');
% Process each match to extract the value of x and store the corresponding damping constant
for i = 1:length(matches)
x = str2double(matches{i}{1});
if x >= 1 && x <= 8
dampingConstants(x) = 1;
end
end
You may refer to the following documentations to learn more about the following functions :
0 Comments
Jaimin
on 13 Aug 2024
Edited: Jaimin
on 13 Aug 2024
I understand that you need to extract the damping constant values for each material stored in"CrPS4.mat” (likely in text format) and store these values in a vector using a MATLAB script.
Here is a script that will be useful to you.
% Open the file for reading
Path = <FILE PATH>;
fileID = fopen(Path, 'r');
% Initialize an empty array to store the damping constants
damping_constants = [];
% Read the file line by line
while ~feof(fileID)
% Get the current line
currentLine = fgetl(fileID);
% Check if the line contains 'damping-constant'
if contains(currentLine, 'damping-constant')
% Split the line by '=' and extract the valu
parts = strsplit(currentLine, '=');
valueStr = strtrim(parts{2});
% Convert the value to a number and append to the array
value = str2double(valueStr);
damping_constants = [damping_constants, value];
end
end
% Close the file
fclose(fileID);
% Display the vector of damping constants
disp(damping_constants);
I hope this resolves the issue.
Here are some useful links
fopen:
fclose:
fgetl:
0 Comments
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!