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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:25:32+00:00 2026-05-26T10:25:32+00:00

A coworker left some data files I want to analyze with Numpy. Each file

  • 0

A coworker left some data files I want to analyze with Numpy.

Each file is a matlab file, say data.m, and have the following formatting (but with a lot more columns and rows):

values = [-24.92 -23.66 -22.55 ;
-24.77 -23.56 -22.45 ;
-24.54 -23.64 -22.56 ;
];

which is the typical explicit matrix creation syntax used by matlab.

My question is: what would be the most practical way to create a numpy array from these files?

I could think about a “brute force” or a “quick and dirty” solution, but if there would be a more straightforward one, I would much rather use it, like a standard function from numpy or even from another module.

EDIT: I noticed that my files may contain NaN values, so I most probably will adapt the answers given to use numpy.genfromtxt instead of numpy.loadtxt. I plan to include my final code as soon as I have it.

Thanks for any help!

EDIT: I ended up with the following code, where I get everything between [] using regex, and create a numpy array using genfromtxt in order to handle NaN. A shorter solution could be to use fromstring method, which does not need StringIO, but this cannot handle NaN, and my data have NaN :oP

#!/usr/bin/env python
# coding: utf-8

import numpy, re, StringIO

with open('data.m') as f:
    s = re.search('\[(.*)\]', f.read(), re.DOTALL).group(1)
    buf = StringIO.StringIO(s)
    a = numpy.genfromtxt(buf, missing_values='NaN', filling_values=numpy.nan)
  • 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-05-26T10:25:32+00:00Added an answer on May 26, 2026 at 10:25 am

    Here are a couple options, although neither is built in.

    The solution you probably do not find acceptable

    This solution probably falls into your “quick and dirty” category, but it helps lead in to the next solution.

    Remove the values = [, the last line (];), and globally replace all ; with nothing to get:

    -24.92 -23.66 -22.55 
    -24.77 -23.56 -22.45 
    -24.54 -23.64 -22.56 
    

    Then you can use numpy’s loadtxt as follows.

    >>> import numpy as np
    >>> A = np.loadtxt('data.m')
    
    >>> A
    array([[-24.92, -23.66, -22.55],
           [-24.77, -23.56, -22.45],
           [-24.54, -23.64, -22.56]])
    

    A solution you might find acceptable

    In this solution, we create a method to coerce the input data into a form that numpy loadtxt likes (the same form as above, actually).

    import StringIO
    import numpy as np
    
    def convert_m(fname):
        with open(fname, 'r') as fin:
            arrstr = fin.read()
        arrstr = arrstr.split('[', 1)[-1] # remove the content up to the first '['
        arrstr = arrstr.rsplit(']', 1)[0] # remove the content after ']'
        arrstr = arrstr.replace(';', '\n') # replace ';' with newline
        return StringIO.StringIO(arrstr)
    

    Now that we have that, do the following.

    >>> np.loadtxt(convert_m('data.m'))
    array([[-24.92, -23.66, -22.55],
           [-24.77, -23.56, -22.45],
           [-24.54, -23.64, -22.56]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My coworker and I have encountered a nasty situation where we have to use
A coworker asked me to look at indexing on some tables because his query
My coworker and I constantly argue about button sizes. I like to have buttons
My coworker suggested making several of the Eclipse code-formatting and warning settings to be
A coworker showed me the following code and asked me why it worked. <span
My coworker has an issue where his commits keep including every single file in
I have a coworker who was no longer work the same office. He has
When I develop code, I typically have a bunch of personal files that I
A coworker used a for loop to iterate a List in some C# code
I have a coworker that is against type inference in C#. I believe most

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.