How to load data into a function

40 ビュー (過去 30 日間)
Adithya Ram Panyam
Adithya Ram Panyam 2022 年 11 月 23 日
回答済み: Florian Bidaud 2022 年 11 月 23 日
Hey all! I have a function which takes input 'x' and 'y' uses these values to find the location of the output 'z' in a set of data(3 data sets) which are stored in the same directory as a .mat file. When I try to run the function, the functions workspace is empty so it does not have the data files. But when I try to load it in the function. I get an error. Is there any other way to load a function into the data? I tried inputting the data as an input into the function and it worked but I do not want to use this method. Thank you Function[z] = hello(x,y) Load(data)------> error Find(....(code continues)
  1 件のコメント
Adithya Ram Panyam
Adithya Ram Panyam 2022 年 11 月 23 日
Sorry wrote this on my phone and I see the code did not enter the next line correctly. After hello(x,y) it's a new line and after 'error'it's a new line

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

回答 (1 件)

Florian Bidaud
Florian Bidaud 2022 年 11 月 23 日
Hi,
If data is a variable, you must include it as a input of your function
function [z] = hello(x,y,data)
load(data)
%Rest of the function
end
The alternative is to delcare data as global variable, but it is not recommanded
global data
function [z] = hello(x,y)
global data
load(data)
%Rest of the function
end

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by