Obtain information after text pattern

2 vues (au cours des 30 derniers jours)
Roderick
Roderick le 13 Août 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?

Réponse acceptée

Walter Roberson
Walter Roberson le 13 Août 2024
S = fileread('CrPS4.mat');
parts = regexp(S, '(?<=damping-constant\s+=\s+)\S+', 'match');
parts_numeric = str2double(parts)
parts_numeric = 1x8
1 1 1 1 1 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Plus de réponses (2)

nick
nick le 13 Août 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 :

Jaimin
Jaimin le 13 Août 2024
Modifié(e) : Jaimin le 13 Août 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:

Catégories

En savoir plus sur Low-Level File I/O dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by