Load particular data in a .m file from another .m file from different folder.

28 ビュー (過去 30 日間)
Vikram Rathore
Vikram Rathore 2019 年 7 月 17 日
コメント済み: Mayur Mhetras 2022 年 4 月 14 日
Hi,
I have two folders in the drive as D:/data/folder1, D:/data/folder2.
In folder1, I created a 'M1.m' file which loads different data of different TYPES and length as A, B, C....
Now I want to use data 'A' in my 'M2.m' file in folder2 and I am trying this
load('D:/data/folder1/M1.m') in M2.m but it gives me an error.
Can you help me to know, How can I access data 'A' in M2.m?
Thanks
  6 件のコメント
Stephen23
Stephen23 2019 年 7 月 17 日
"All the preceding variables I defined in M2.m lost from the workspace."'
There are multiple reasons why that might happen:
  • your code does not pass the required values as input/output arguments.
  • your code calls clear.
  • you are looking in the wrong workspace.
  • That variable is not actually created.
  • etc.
If you do not show us the code that you are trying to run we can only make guesses.
Vikram Rathore
Vikram Rathore 2019 年 7 月 17 日
I didn't call clear but now I put 'run ..' in the beginning and then defined rest of the variables. It is working fine now(So silly I am!!).
Thanks a lot,
Regards,

サインインしてコメントする。

採用された回答

Rik
Rik 2019 年 7 月 17 日
I'm going to guess that your m file is not a function but a script. I would recommend using functions for anything but testing. That way you can make your code modular. The function below will execute the script and return the A variable.
function A=getA
%run the script and return the A variable
run('D:/data/folder1/M1.m')
end
You shouldn't use this as a structural solution.
  2 件のコメント
Vikram Rathore
Vikram Rathore 2019 年 7 月 17 日
Thanks a lot, this also worked for me.
Best Regards
Mayur Mhetras
Mayur Mhetras 2022 年 4 月 14 日
Thank you Sir for your answer. I was also having similar dought and it solved using your answer.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by