-1

I have to convert some small codes of my friend from Matlab to Python, I was trying to find a good translation of "assign" word in Matlab, but Unfortunately with no answer. Would you please help me with this piece of code in Matlab and translated it to Python, I got stuck at the last line especially. Thanks for your help

function importfile1(fileToRead1)

%IMPORTFILE1(FILETOREAD1)%  Imports data from the specified file %  FILETOREAD1:  file to read % Import the file

newData1 = load('-mat', fileToRead1);

% Create new variables in the base workspace from those fields.

vars = fieldnames(newData1);
for i = 1:length(vars)
    assignin('base', vars{i}, newData1.(vars{i}));
end
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Farideh
  • 7
  • 2
  • 4
    Python does not have an equivalent. But even in MATLAB this is bad practice. – Cris Luengo Aug 15 '22 at 16:06
  • Yeah this is horrible MATLAB code. Don't do this. – Ander Biguri Aug 15 '22 at 17:22
  • How about using `=`? – dmedine Aug 16 '22 at 00:20
  • @Cris Luengo ,Ander Biguri Would you please let me know more, about why it doesn't sound good? as my professor said to convert/translate some other Matlab codes like this to Python. -dmedine, would you please let me know more to write it completely? Thanks – Farideh Aug 17 '22 at 12:41
  • Code translation requests [are generally off topic due to needing more focus](https://meta.stackoverflow.com/questions/265825). Please use *complete English sentences* to explain *what should happen* when the Python code runs. – Karl Knechtel Sep 13 '22 at 14:53

1 Answers1

1

I agree with the comments that this is somewhat bad practice, and also that the two are sufficiently different that this doesn't entirely make sense, but we can attempt the best analog. Python doesn't use the same "workspace" terminology (and some things likely don't cross over neatly), but from the docs it would seem that assignin is used largely to assign values to variables which aren't currently in-scope by accessing either the "base" scope (global), or other function scopes. In python you can (within a function) indicate that you want to use the global version of a variable with the global keyword, then simply assign a value to it as normal: global myvar then myvar = "some_value". If you have both a global version as well as one of the same name in an enclosing scope, and you want to access one level higher, but not necessarily the global scope you instead use the keyword nonlocal.

The other thing this function does is allow you to create variable names programmatically which is generally considered bad practice, as your variable names should generally be fixed, and not variable themselves. In this instance, it would seem the code may be replicating the functionality of saving all the variables from an interactive session, then re-loading them later in a new interactive session. In this case the general wisdom of not having "variable variables" is less applicable, and indeed makes sense over the general solution to "variable variables" which is a dictionary (or other data structure). The actual solution to creating new variables programmatically is the eval function, which for many reasons is generally avoided, but particularly for interacting with information from a live coding session may be appropriate. In this case you would build a string representing the appropriate python expression such as:

eval(str(vars[i]) + "=" + str(newData1[vars[i]]))

*note you will have to spend some time to figure out how to make sure you're building the correct expression for eval this is a very rough example.

Aaron
  • 10,133
  • 1
  • 24
  • 40