Use variable in two different functions

I have to use variable defined in one function in another function. What should I use? I tried global but it fails with me

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 3 日
編集済み: Azzi Abdelmalek 2016 年 4 月 3 日

0 投票

[var1,...]=fcn1(...)
get the variable var1 from fcn1 and use it in fcn2
[...]=fcn2(var1,...)

5 件のコメント

Ali Jaber
Ali Jaber 2016 年 4 月 3 日
I didn't get the idea.. Please can you give me an example
dpb
dpb 2016 年 4 月 3 日
As Azzi says, just use the first function to create/return the first variable and pass that into the second function call.
If that's not clear enough show us your problem instead...and what you tried.
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 3 日
編集済み: Azzi Abdelmalek 2016 年 4 月 3 日
%function 1
function y=fcn1(u)
var1=2*u
y=vars1+5
% function 2
function s=fcn2(x)
s=sin(x)^2
Now you need to use the variable var1, you have to modify the function fcn1
fucntion [var1,y]=fcn1(u)
var1=2*u
y=vars1+5
now you can get the variable var1 from fcn1
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 3 日
You could precise that you are using Guide Matlab. you can share data with guidata function
function btnVisualDataset_Callback(hObject, eventdata, handles)
[filename, pathname] = uigetfile('*.xlsx','Load Dataset');
if filename==0
msgbox('Please Choose Dataset','Error','Error')
return
end
for i=1:10
data = xlsread(filename);
mv{i}=[data(:,i)];
handles.mv=mv;
guidata(hObject,handles)
end
function btnSearch_Callback(hObject, eventdata, handles)
mv=handles.mv
end
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 4 月 3 日
You already get the answer

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by