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 9019189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:43:01+00:00 2026-06-16T04:43:01+00:00

I have a textfile that I need to read into matlab. I want to

  • 0

I have a textfile that I need to read into matlab. I want to implement something like a java LinkedHashMap in Matlab using structures. my textfile is like this

3-1 33.33 37.58

3-1 66.67 20.47

3-2 33.33 41.64

3-2 66.67 24.42

I read the entire file into array [a,x,y] where a is a cell array containing 3-1 ad 3-2

I need the structure to have its field names as a(1) or a(2) but unfortunately matlab gives me error. The reason is that I need to check if for instance a(1) is already a structure field name I concat the values to the previous values; if not, make a new field name with the respected values. the code is:

[a,x,y]=textread('mytxt.txt', '%q%f%f','commentstyle','matlab');

s.a(1)=[x(1),y(1)];

for j=2:length(a)

    if isfield(s,a(1))==0

        s.a(j)=[x(j),y(j)];

     else

        temp = s.a(j);

        C = concat(1,temp,[x(j),y(j)];

        s.a(j) = C

    end

end 
  • 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-16T04:43:02+00:00Added an answer on June 16, 2026 at 4:43 am

    Presumably you want to have s as a structure with fields 3-1, 3-2, …, 3-n. I also think you want to create using MATLAB’s dynamic field names.

    To access the fields dynamically through the cells in the cell-array a you need to do something similar to:

    s.(a{j}) = [x(j), y(j)];
    

    Make a note of the different uses of parentheses () and braces {}. The parentheses are for indicating dynamic field-names as in s.(str) where str is a character array. The braces are used to index into the cell array a as in a{j} gives the character array of the jth cell in a.

    Finally, your proposed field-names (3-1, 3-2, … etc) are not legal MATLAB field-names because of the hyphen ‘-‘ and they do not begin with a letter, [a-zA-Z]. So, you need to replace the hyphen with another character, for example an underscore and append a letter to the start of the proposed field-name.

    1. Replacing hyphen using strrep as in

      a=strrep(a,’-‘,’_’);

    2. Appending a letter to the start of the field-name

      a=cellfun(@(str) [‘a_’ str],a,’uniformoutput’,0)

    This work can be done right after you finish reading the file into matrices a, x, and y

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

Sidebar

Related Questions

I have a text file that I want to edit using Java. It has
I have this text file that I read into a Java application and then
I have a plain text file that I need to read in using C#,
i have a remote text file on http://somewhere ... that i need to read
I have a text file I would like to read in that contains rows
I have a Java program that reads lines from text file. I want to
I have a text file like 11111 10001 10001 11111 I need to read
So, I have a text file that is organized like this <username>:<fullname>:<usergroups> I need
I have a text file dump that I need to convert to a delimited
I have a text file having words that i need to validate. After following

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.