AppDesigner Code Simplification Help

2 views (last 30 days)
Carlos Garciamendez
Carlos Garciamendez on 25 Mar 2021
Answered: Vimal Rathod on 19 May 2021
Hello all, currently developed a program that runs well in AppDesigner. However, I am finding that a lot of my code is copy pasted several times (around 5-6).
Therefore, my question is to see if there is a way to create a varible/function that stores all of these lines are code that are repeated several times?
For reference here is an example of a segment of code that is repeated:
% Define the SLM struct
SLM.window = Screen('Preference', 'VisualDebuglevel', 3);
SLM.window = Screen('Preference','SkipSyncTests',2); % Skips the showing of Pink Screen
black = BlackIndex(2); % Assign a black index to 2nd screen
SLM.window = Screen('OpenWindow',2,black); % Opens the 2nd screen and shows black
% Create blank image
black_background = zeros(1920,1080); % Assigns Zeros to all background matrix to show black
blank_image_ptr = Screen('OpenOffscreenWindow',SLM.window,2); % Opens an off screen i.e. background screen on 2nd window
% textureIndex = Screen('MakeTexture', blank_image_ptr, black_background);
% Screen('DrawTexture', blank_image_ptr, textureIndex);
Screen('PutImage',blank_image_ptr,black_background); % Assign black background to blank image ptr in Off screen
% Screen(SLM.window,'DrawTexture',black_background); % start by projection of a blank image
Screen(SLM.window,'PutImage',black_background); % start by projection of a black background on off screen
Screen(SLM.window,'Flip'); % Project image on on_screen
% First create image pointers
window_ptrs = zeros(1,image_total); % vector for storing the pointers to each image
Also in the last line, "images_total" is an input that is taken from a text field in the GUI.

Answers (1)

Vimal Rathod
Vimal Rathod on 19 May 2021
With the information given, I am assuming that you want to know how to package the above snippet of code in a function and reuse it. There are two ways I could think of as a solution to your question here. First is to create a function put the above snippet of code and as a parameter to the function give "images_total" or any variable and pass it to the function when calling it. The second way is only when you want to run your code snippet in certain events for that you could use callbacks in app designer.

Community Treasure Hunt

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

Start Hunting!