MATLAB help - searching text for specific words

In summary, the conversation is about a physics major seeking help with writing a program in MATLAB to analyze data for biophysics research. The program needs to read a text file, search for keywords, and store the strings after those keywords. The speaker is new to MATLAB and is unsure about using the textscan function. The expert suggests using delimitedTextImportOptions and convertCharsToStrings to read the file and then compare the elements using strcmpi function. It is also mentioned that dlmread function should not be used as per MATLAB's recommendation.
  • #1
BakerIregular
1
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
  • #2
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:

1. How do I search for specific words in my MATLAB program?

To search for specific words in your MATLAB program, you can use the "find" function. This function allows you to specify a word or phrase to search for within your code, and it will return the line numbers where that word or phrase appears. You can also use regular expressions to search for patterns of text.

2. Can I search for multiple words at once in MATLAB?

Yes, you can search for multiple words at once in MATLAB using the "find" function. Simply separate the words with a space, and the function will return all lines where any of the specified words appear.

3. How can I search for words in a specific file or folder in MATLAB?

To search for words in a specific file or folder in MATLAB, you can use the "dir" function to list the files in a given directory. Then, you can use the "find" function to search for words within each of those files.

4. Is there a way to search for words in comments or documentation in MATLAB?

Yes, you can search for words in comments or documentation in MATLAB by using the "help" command. This will bring up the documentation for a specific function or keyword, and you can use the "find" function to search within that documentation for specific words.

5. How can I save my search results for future reference in MATLAB?

To save your search results for future reference in MATLAB, you can use the "export" function to save the results as a .txt or .csv file. This will allow you to open and view the results in a separate document outside of MATLAB.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
29
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Programming and Computer Science
2
Replies
41
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
Back
Top