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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:01:18+00:00 2026-06-14T07:01:18+00:00

If we consider the following generator function … def loadrun(runs, varnames=None): for fle in

  • 0

If we consider the following generator function …

def loadrun(runs, varnames=None):
    for fle in runs:
        yield loadmat(fle, variable_names=varnames)

which we call as follows …

vnames = ['targetpos', 'cuepos', 'soa', 'response']
trials = loadrun(datafiles, vnames)  # datafiles is a list of paths to files

we get the following output when listing the length of the resulting dict:

[in] for t in trials:
         print len(t)

[out] 7
      128
      128
      128
      128
      128
      128
      128

All .mat files contain 124 variables, so only the first output is correct. What gives?

EDIT

I further reduced the scope of the problem. It seems as though loadmat‘s variable_names field fails after the first iteration in any loop. Any idea what’s going on here?

for df in datafiles:
    print len(loadmat(df, variable_names=vnames))

[out] 7
      128
      128
      128
      128
      128
      128
      128
  • 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-14T07:01:20+00:00Added an answer on June 14, 2026 at 7:01 am

    scipy.io.loadmat is being naughty[1] and mutating the list passed to variable_names. After the first call to loadmat, varnames is the empty list.

    You can workaround this by defining a temporary variable, _varnames:

    def loadrun(runs, varnames = None):
        for fle in runs:
            _varnames = None if varnames is None else varnames[:]
            yield sio.loadmat(fle, variable_names = _varnames)
    

    For example:

    import string
    import scipy.io as sio
    import numpy as np
    
    letters = string.ascii_lowercase
    datafiles = ['/tmp/data{i:d}.mat'.format(i = i) for i in range(3)]
    
    def loadrun(runs, varnames = None):
        for fle in runs:
            _varnames = None if varnames is None else varnames[:]
            yield sio.loadmat(fle, variable_names = _varnames)
    
    def create_datafiles():
        for filename in datafiles:
            arr = np.arange(9).reshape((3, 3))
            mdict = {letter : arr for letter in letters}
            sio.savemat(filename, mdict = mdict)
    
    create_datafiles()
    vnames = ['a', 'c', 'd']
    trials = loadrun(datafiles, vnames)
    for t in trials:
        print(t.keys())
    

    Actually, it appears the problem originates in both mio4.py and mio5.py, where MatFile4Reader and MatFile5Reader calls the remove method on variable_names:

    def get_variables(self, variable_names=None):
        while not self.end_of_stream():
            ...
            if variable_names:
                variable_names.remove(name)
                if len(variable_names) == 0:
                    break
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's consider the following scenario: a function which can generate code colors from white
Consider the following function: void f(const char* str); Suppose I want to generate a
Consider the following Javascript code: var a = []; var f = function() {
Consider the following definition of a category: trait Category[~>[_, _]] { def id[A]: A
Consider the following NEON-optimized function: void mat44_multiply_neon(float32x4x4_t& result, const float32x4x4_t& a, const float32x4x4_t& b)
Consider the following hibernate configuration: <class name=Person> <id name=id column=personId> <generator class=native/> </id> <set
Consider the following Python code: def f(*args): for a in args: pass foo =
Consider following make: all: a b a: echo a exit 1 b: echo b
Consider following code: My problem is: 1) I can't seem to cast the errors
Consider following string Some string with quotes and \pre-slashed\ quotes Using regex, I want

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.