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

10 views (last 30 days)
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

Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!