How to save a workspace variable from the matlab code itself?

4 Ansichten (letzte 30 Tage)
I want to save a workspace variable from the matlab code itself. I am able to save the workspace variable using save command in command window but I do not want to give any command in the command window . I want to save the workspace vaiable with a command given in the matlab code itself. I tried save command or matlab.io.saveVariablesToScript() in the matlab code but it gives the error variable 'ans' not found ('ans' isthe workspace variable I want to save).
I am running this matlab code from python using matlab.engine so I can not give any command in the command window.
I want to know three things.
1) Can I save a workspace variable from python after it's run?
2) Can I save workspace variable from the command given in the matlab code itself?
3) Can I give which command needs to be run in the command window from the matlab code itself?
I tried first two but they gave the error. I have no idea about the third one.
Can somebody help me please?
  3 Kommentare
CHINTALA NIRMAL SRINIVAS
CHINTALA NIRMAL SRINIVAS am 18 Mai 2020
save('path of the mat file','variable')
save('path of the mat file',variable)
matlab.io.saveVariablesToScript('path of the mat file',variable)
Rik
Rik am 18 Mai 2020
The top one should have worked, if you are indeed in a Matlab environment.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 18 Mai 2020
Doing
save('answers.mat', 'yourVariableName');
should work from either the command window, or from your script or function (m-file). Not sure about Python, but if your variable is in scope (in the current workspace when save() is called), then this should work. Change the mat filename to whatever you want, and of course change the variable name.
  2 Kommentare
CHINTALA NIRMAL SRINIVAS
CHINTALA NIRMAL SRINIVAS am 18 Mai 2020
It says the variable is not found. But it produces the workspace variable when it runs. Could you please tell me what could be the reason?
CHINTALA NIRMAL SRINIVAS
CHINTALA NIRMAL SRINIVAS am 18 Mai 2020
Got it .I was giving the variable name which is being displayed on the workspace but in the code the variable name is different. After I changed the name, it worked

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by