MATLAB help - searching text for specific words

  • Context: MATLAB 
  • Thread starter Thread starter BakerIregular
  • Start date Start date
  • Tags Tags
    Matlab Specific Text
Click For Summary
SUMMARY

The discussion focuses on using MATLAB to read a text file and extract specific keywords and their associated strings for data analysis in biophysics research. The user seeks assistance with the textscan function but is guided to use readmatrix with delimitedTextImportOptions instead. The provided code demonstrates how to read the text file, convert it to a string array, and utilize strcmpi for keyword comparison. It is noted that while dlmread could be used, it is deprecated as of MATLAB R2019a.

PREREQUISITES
  • Basic understanding of MATLAB programming
  • Familiarity with text file formats and delimiters
  • Knowledge of MATLAB functions like readmatrix and strcmpi
  • Awareness of MATLAB's data types, specifically cell arrays and string arrays
NEXT STEPS
  • Explore the textscan function for advanced text parsing in MATLAB
  • Learn about delimitedTextImportOptions for customizing data import settings
  • Investigate the differences between readmatrix and dlmread
  • Study the use of loops and conditional statements in MATLAB for data manipulation
USEFUL FOR

This discussion is beneficial for undergraduate students in physics, researchers in biophysics, and MATLAB users looking to enhance their skills in text file data extraction and analysis.

BakerIregular
Messages
1
Reaction score
0
Hello! I stumbled upon these forums and it looks like this is a good place to get an answer to this question. I am an undergrad physics major doing biophysics research and writing a program to help with data analysis. The first step it to write a program in MATLAB that reads in a text file, will search for keywords, and store the strings after those keywords.
Example, the file would look like:


Test
Test Name
Group
Group 1
Group
Group2
Subject
Subject1
Property1


I need to look through this file. When I get to test, I read the test name, store it, then when it gets to group, read the group name, store it, look for subjects, read subject name and some other properties, store them, then look for another subject, then for another group, then for another test. My issue is reading the strings looking for these keywords, then storing the words after them. I think the textscan function is what I need to use but I am new to MATLAB programming and not sure how exactly it works. Any help would be greatly appreciated!

Thanks!
 
Physics news on Phys.org
This can be done assuming that the file format is strictly what you have written (the important factor is the presence of %n).

First the code:
Matlab:
opts = delimitedTextImportOptions('Delimiter','%n');
A = readmatrix('text.txt',opts); %read the text file
A = convertCharsToStrings(A); %before this, A is a cell array
Basically you are telling Matlab that as soon as it encounters a new line character (%n), it will read the consecutive string into another cell of the matrix A. Now, A is a cell array. So, convert it to a string array.

Now you can compare the elements of A as required using strcmpi function:
Matlab:
for i=1:size(A)
    if(1 == strcmpi(B(i), "group"))
        %do necessary operations on B(i+1) and B(i+2)
        %as required
        i = i+1;
        %change 1 to 2 or 3 if you have operated
        %on i+2 and i+3, so on...
    end
end

The documentations are as follows:
readmatrix function:
https://in.mathworks.com/help/matlab/ref/readmatrix.htmlUsing opts:
https://in.mathworks.com/help/matla...d40-4b04-addc-3a5de11f672a_sep_btx_238-1-optshttps://in.mathworks.com/help/matlab/ref/matlab.io.text.delimitedtextimportoptions.htmlImportant point:
Instead of readmatrix, this work could be done using dlmread function. But as of R2019a, MATLAB is requesting users not to use it.
 
Last edited:

Similar threads

Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
2
Views
3K
Replies
1
Views
2K
Replies
1
Views
5K
  • · Replies 29 ·
Replies
29
Views
5K
  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K