MATLAB MATLAB help - searching text for specific words

AI Thread Summary
An undergraduate physics major conducting biophysics research seeks assistance in writing a MATLAB program to analyze data from a text file. The program needs to read the file, identify keywords, and store the corresponding strings. The suggested approach involves using the `readmatrix` function with `delimitedTextImportOptions` to define the delimiter as a new line character (%n). This allows the program to read and store strings into a cell array, which can then be converted to a string array. The `strcmpi` function is recommended for comparing elements and performing operations based on the identified keywords, such as "group" and "subject." While `dlmread` could also be used, it is advised against due to MATLAB's recommendation to avoid it as of R2019a. Documentation links for `readmatrix` and related options are provided for further guidance.
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:
Back
Top