Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7804613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:58:20+00:00 2026-06-02T01:58:20+00:00

I am reading a text file into Matlab called ‘test.txt’ which is structured as

  • 0

I am reading a text file into Matlab called ‘test.txt’ which is structured as follows:

$variable1 = answer1; 
$variable2 = answer2;
$variable3 = answer3;

I read the text file into Matlab line by line using the following segment of code:

fid = fopen('test.txt.');
tline = fgetl(fid);
tracks = {};
while ischar(tline)
    tracks{end+1} = regexp(tline, '(?<=^.*\=\s*)(.*)(?=\s*;$)', 'match', 'once');
    tline = fgetl(fid);
end
fclose(fid);

This piece of code returns the value of each variable line by line and would output:

answer1
answer2
answer3

What I want to do is modify my regexp expression so that I can specify the name of the variable to retrieve and have Matlab output the value assigned to the variable specified.

E.g. If I specify in my code to find the value of $variable2, Matlab would return:

answer2

Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:58:22+00:00Added an answer on June 2, 2026 at 1:58 am

    One possible solution:

    function [tracks] = GetAnswer(Filename, VariableName)
    fid = fopen(Filename);
    tline = fgetl(fid);
    tracks = {};
    
    % prefix all $ in VariableName with \ for `regexp` and `regexprep`
    VariableName = regexprep(VariableName, '\$', '\\$');
    
    while ischar(tline)
        if (regexp(tline, [ '(', VariableName, ')', '( = )', '(.*)', '(;)' ]))
            tracks{end+1} = regexprep(tline, [ '(', VariableName, ')', '( = )', '(.*)', '(;)' ], '$3');
            % if you want all matches (not only the 1st one),
            % remove the following `break` line.
            break;
        end
        tline = fgetl(fid);
    end
    
    fclose(fid);
    return
    

    You can call it this way:

    Answer = GetAnswer('test.txt', '$variable2')
    
    Answer = 
    'answer2'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading and writting a structure into a text file which is not
I am reading a text file which contains numbers in the range [1, 10^100].
I am reading a text file which I guess is encoded in utf-8. Some
I need some help reading data from a text file into my ArrayList .
I'm having a problem reading data from a text file into ms sql. I
I'm reading a text file line by line, and storing it into a NSArray.
So I'm attempting to create a Befunge interperter and reading a text file into
I am reading a text file which contains a word with a punctuation mark
What would be the most efficient method of reading a text file into a
I generate the XML by reading the text form .txt file. But I got

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.