Loading Images from a different folder into App on Startup

조회 수: 16 (최근 30일)
Andre Aroyan
Andre Aroyan 2023년 8월 4일
댓글: Rik 2023년 8월 8일
I'm using the Matlab App Designer in Matlab 2020b, and I would like to have some buttons use an icon. I have the .png images for the icons in a folder within the directory of the .mlapp file. However, when I open an instance of Matlab and first load the app in Design View, the images are not found. I get the following error.
"Warning: File 'Down Arroow.png' is not on the MATLAB or specified path."
In the startupFcn of the app I added the following code.
addpath([pwd '\Assets\Sprites\'])
This is the path to the images from the run directory of the mlapp file.
When I run the app for the first time, the images also do not load.However, if I close the app and run again, the images do load. Furthermore, when I close the mlapp and re-open in the App Designer, the images will load in Design View.
Is there a way automatically load the images the first time I open the .mlapp file, without just putting all the images in the same directory as the malpp file? Alternatively, is there a way to make sure the images load when I press run for the first time? Would any of this cause problems if I decide to make the app an executable?

답변 (1개)

Rik
Rik 2023년 8월 4일

You might want to set the parent directory as a variable in AppDesigner, and then use fullfile to generate the file names based on that.

That way you are not modifying the path after relying on it (which is the cause of the errors here). Not modifying the path is always best.

  댓글 수: 2
Andre Aroyan
Andre Aroyan 2023년 8월 7일
How would I create a parent directory variable before it used? The icons are used in the createComponents function, and it seems to me like the first place I can any code is the startupFcn, which is executed after createComponents. Is there a way to do it in the Designer GUI?
Rik
Rik 2023년 8월 8일
Since you're currently using pwd, you could use something like this:
app.pwd = pwd;
Then, when loading an image, you can use code like this:
im = imread(fullfile(app.pwd,'Assets','Sprites','Down Arroow.png'));
That way you can easily modify the parent directory in one place and you don't need to modify the path.
You will need to consider the case where your GUI will be loaded when it is not in the current directory.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by