Read Information of Line with keyword from File
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hey Guys,
I want to make MATLAB go through a file with a certain format and look for two lines with a keyword at the beginning of the specific line. Then I want to read 15 values of each lines and hand them over into a matrix to then use them in my code. The problem with this file is that the lines with the information is not always the same (but the two lines are always following onto each other). So my code for a static line doesn't work anymore. The file looks like in the attachment. It may not look like it's in order but on Notepad++ it has defined columns. I want every of the 15 values in the line after 'maximale Spannung [N/mm^2]:' in the second row of a matrix and every of the 15 values in the line after 'mittlere Spannung [N/mm^2]:' in the third row of the same matrix so like
Matrix = [35.795736 66.896039 98.314700 130.349492 162.211252 194.046744 225.733955 257.277022 288.707962 319.766213 350.105963 380.476533 411.354555 441.572844 472.535866;
22.816152 43.566331 64.107899 84.613975 104.955023 125.188096 145.313886 165.358105 185.353835 205.246390 224.970758 244.671696 264.307086 283.826201 303.379865]
I am really having trouble understanding the syntax of filescan or regexp. I hope anyone can help me. Thanks!!
0 Commenti
Risposte (1)
Suraj Mankulangara
il 21 Feb 2018
Hello Simon
You can use the fgets() or fgetl() functions from MATLAB to read the file line-by-line into an array. These will read the entire line of content until a newline characters is reached (regardless of how it displays on Notepad) You can then use the strsplit() function with ':' as delimiter to split the resulting line. The following code should give you an idea:
fileID = fopen('File_format.txt','r');
while ~feof(fileID)
A = fgetl(fileID);
%class(A)
res = strsplit(A, ':')
end
For the rows that begin with 'maximale Spannung [N/mm^2]:' or 'mittlere Spannung [N/mm^2]:' the variable res will be a 1x2 cell array. The 2nd column of this cell array contains the 15 values that you want to read. You can read it using the command res{2}.
You will need to use strsplit() again, with space as delimiter, in order to convert the character array to a vector.
Sincerely,
Suraj Mankulangara
0 Commenti
Vedere anche
Categorie
Scopri di più su Large Files and Big Data in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!