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

  • SEARCH
  • Home
  • 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 8496591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:53:47+00:00 2026-06-10T23:53:47+00:00

I everybody, I’m trying to parse a text file into matlab: it consists of

  • 0

I everybody,

I’m trying to parse a text file into matlab: it consists of several blocks (START_BLOCK/END_BLOCK) where are allocated strings (variable) and values (associated to the previous variables).

An example is this:

START_BLOCK_EXTREMEWIND
velocity_v1 29.7


velocity_v50    44.8


velocity_vred1  32.67
velocity_vred50 49.28


velocity_ve1    37.9



velocity_ve50   57


velocity_vref   50



END_BLOCK_EXTREMEWIND

Currently, my code is:

fid = fopen('test_struct.txt','rt');
C = textscan(fid,'%s %f32 %*[^\n]','CollectOutput',true);
C{1} = reshape(C{1},1,numel(C{1}));
C{2} = reshape(C{2},1,numel(C{2}));



startIdx = find(~cellfun(@isempty, regexp(C{1}, 'START_BLOCK_', 'match')));
endIdx = find(~cellfun(@isempty, regexp(C{1}, 'END_BLOCK_', 'match')));
assert(all(size(startIdx) == size(endIdx)))
extract_parameters = @(n)({C{1}{startIdx(n)+1:endIdx(n) - 1}});
parameters = arrayfun(extract_parameters, 1:numel(startIdx), 'UniformOutput', false);

s = cell2struct(cell(size(parameters{1})),parameters{1}(1:numel(parameters{1})),2);

s.velocity_v1 = C{2}(2);
s.velocity_v50 = C{2}(3);
s.velocity_vred1 = C{2}(4);
s.velocity_vred50 = C{2}(5);
s.velocity_ve1 = C{2}(6);
s.velocity_ve50 = C{2}(7);
s.velocity_vref = C{2}(8);

It works, but it’s absolutely static. I would rather have a code able to:

1. check the existence of blocks --> as already implemented;
2. the strings are to be taken as fields of the structure;
3. the numbers are meant to be the attributes of each field.

Finally, if there is more than one block, there should be and iteration about those blocks to get the whole structure.
It’s the first time I approach structure coding at all, so please be patient.

I thank you all in advance.

Kindest 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-10T23:53:49+00:00Added an answer on June 10, 2026 at 11:53 pm

    It sounds like you will want to make use of dynamic field names. If you have a struct s, a string fieldName that stores the name of a field, and fieldVal which holds the value that you’d like to set for this field, then you can use the following syntax to perform the assignment:

    s.(fieldName) = fieldVal;
    

    This MATLAB doc provides further info.

    With this in mind, I took a slightly different approach to parse the text. I iterated through the text with a for loop. Although for loops are sometimes frowned upon in MATLAB (since MATLAB is optimized for vectorized operations), I think in this case it helps to make the code cleaner. Furthermore, my understanding is that if you are having to make use of arrayfun, then replacing this with a for loop probably won’t really cause much of a performance hit, anyway.

    The following code will convert each block in the text to a struct with the specified fields and values. These resulting “block” structs are then added to a higher-level “result” struct.

    fid = fopen('test_struct.txt','rt');
    C = textscan(fid,'%s %f32 %*[^\n]','CollectOutput',true);
    fclose(fid);
    
    paramNames = C{1};
    paramVals = C{2};
    
    curBlockName = [];
    inBlock = 0;
    blockCount = 0;
    
    %// Iterate through all of the entries in "paramNames".  Each block will be a
    %// new struct that is then added to a high-level "result" struct.
    for i=1:length(paramNames)
        curParamName = paramNames{i};
        isStart = ~isempty(regexp(curParamName, 'START_BLOCK_', 'match'));
        isEnd = ~isempty(regexp(curParamName, 'END_BLOCK_', 'match'));
    
        %// If at the start of a new block, create a new struct with a single
        %// field - the BlockName (as specified by the text after "START_BLOCK_"
        if(isStart)
            assert(inBlock == 0);
            curBlockName = curParamName(length('START_BLOCK_') + 1:end);
            inBlock = 1;
            blockCount = blockCount + 1;
            s = struct('BlockName', curBlockName);          
    
        %// If at the end of a block, add the struct that we've just populated to
        %// our high-level "result" struct.
        elseif(isEnd)
            assert(inBlock == 1);
            inBlock = 0;
            %// EDIT - storing result in "structure of structures"
            %//  rather than array of structs
            %// s_array(blockCount) = s;
            result.(curBlockName) = s;
    
        %// Otherwise, assume that we are inside of a block, so add the current
        %// parameter to the struct.
        else
            assert(inBlock == 1);
            s.(curParamName) = paramVals(i);
        end
    end
    
    %// Results stored in "result" structure
    

    Hopefully this answers your question… or at least provides some helpful hints.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hy everybody! I'm trying to use Childbrowser Phonegap plugin to redirect the user within
everybody! I'm trying to digitally sign some data in C#. Everything goes without error
Hi everybody i want to display some HTML file in a visual basic 6
Hello everybody I am trying to change the template of a column in my
Everybody, I am a Python/C# guy and I am trying to learn C++. In
Hello Everybody! I got the following error, while trying to test a code for
hi everybody out there i need help from u in file handling I'm using
Everybody have to change the settings.py file every time you start a new django
Everybody, I would like to ask you what is the most useful software to
everybody! I draw a plot based on some data in my app. The plot

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.