How to use a variavle calculate by a code to another code?

2 ビュー (過去 30 日間)
Mohammed Lamine Mekhalfia
Mohammed Lamine Mekhalfia 2022 年 1 月 18 日
コメント済み: Stephen23 2022 年 1 月 18 日
Hi all,
I hope that everyone is doing well.
I have a code this code calculate the value of a variable y.
I would like to export y only to another code and I do not wana use sub function because both codes are long and I may used several same characters at different stage.
Thanks and regards
  1 件のコメント
Stephen23
Stephen23 2022 年 1 月 18 日
The simple, efficient, and most robust way to pass data between function workspaces is to pass input/output arguments:

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

回答 (2 件)

Max Heimann
Max Heimann 2022 年 1 月 18 日
編集済み: Max Heimann 2022 年 1 月 18 日
Im not sure i get your point about long code and reused variables. Its also a bit unclear what kind of code we are talking about.
Do you have 2 scripts script_A and script_B and want to calculate variable y in sript_A and then use it in script_B? In that case script_A will write y into the matlab base-workspace where it will be available to any code that is run after it, including script_B.
script_A
y = rand;
script_B
z = y;
Now you could type in the console:
>>script_A
>>script_B
In case we are talking about functions (which do not have direct access to the base-workspace) you could define y as a function return value.
function y = function_A
y = rand
end
And now you can use y in another script with
z = function_A;

Mohammed Lamine Mekhalfia
Mohammed Lamine Mekhalfia 2022 年 1 月 18 日
Thank you a lot, it is exactly what I am looking for.
but what I want to do is to clear all the variable in script A except for y that I will use it later in script b.
I meant by long is that I used a lot of abreviation for variables and I repeated it in scrtipt B this is why I wanna clear all except for y.
  1 件のコメント
Max Heimann
Max Heimann 2022 年 1 月 18 日
In that case i would suggest you simply turn script_A into a function and only return the variables you need. Functions have a seperate workspace from the base workspace and you wont have to clear those variables manually once the function is finished.

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

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by