Import doesn't works when I use them in the 'startupFcn()' of APP designer

3 次查看(过去 30 天)
I have imported matlab.http.* and other HTTP packages in the startupFcn, but when other method (function) are called, it still prompts error messages showing that I didn't import them ... I have to import them in EVERY methods that will use those packages... BUT I think this approach sucks ... Anyone who can give me a simpler and reasonable way to solve this? My code is attached below...
startupFcn()
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
import matlab.net.*
import matlab.net.http.*
import matlab.net.http.io.*
disp("1. Matlab.net tools loaded ...")
api_key = string(fileread("KEY.txt"));
if api_key ~= ""
disp(["2. API key loaded: "+ api_key]);
else
disp("Please Provide your API KEY by editing 'KEY.txt'.");
end
% Setup hyper parameters when startup
app.attempt_counter = 0;
app.max_attempt = 2;
app.max_tokens = 1024;
app.n_solution = 1;
app.prompt = "";
% Setup HTTP request header
app.header = [
HeaderField('Content-Type', 'application/json')
HeaderField('Authorization', ['Bearer ' + api_key])
]';
disp("3. Header generated: ");
disp(app.header(2).Value);
end
Other function that will use HTTP (I have to import them again)
% Value changed function: ModelDropDown
function ModelDropDownValueChanged(app, event)
import matlab.net.*
import matlab.net.http.*
import matlab.net.http.io.*
selected_model = app.ModelDropDown.Value;
if selected_model == "GPT3.5Turbo"
app.model = 'gpt-3.5-turbo';
app.endpoint = "https://api.openai.com/v1/chat/completions";
disp(app.endpoint);
elseif selected_model == "GPT3.5"
app.model = 'gpt-3.5-0301';
else
app.model = 'davinci-v3';
end
disp("Selected Model is: " + app.model);
end

回答(1 个)

Kanishk
Kanishk 2024-10-14
Hi Dyson,
I understand you want to import packages in every method by importing them once.
In MATLAB, the import statement is scoped to the function or script in which it is called. This means that if you use import within a function, it won't be available in other functions unless you import it again in each of those functions. MATLAB does not have a global import feature like some other programming languages. You can read more about it in the following MATLAB answer.
However, you can take one of following approaches to simplify the usage of packages across different methods in the app:
  • Usage of fully qualified names instead of using import statements in every method.
response = matlab.net.http.RequestMessage;
  • Declaration of helper functions which encapsulates the functionality of the package. You need to import the package once and use the helper function.
methods (Access = private)
function response = baseHttpFunction(app, uri)
import matlab.net.http.*
request = RequestMessage();
response = request.send(uri);
end
function response = anotherFunction(app, uri)
response = app.baseHttpFunction(uri);
end
end
To learn about importing packages, please go through this official MATLAB documentation.
Hope this helps!
Thanks

类别

Help CenterFile Exchange 中查找有关 Call Web Services from MATLAB Using HTTP 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by