Open an image when push button is clicked in app designer

HI !
I have a push button in the app. I want the push button to open an image file ( file name will be pre-defined in the code) .
I do not want to browse the image file.
What syntax should I use to get this worked ?

 Accepted Answer

Eli Chen
Eli Chen on 12 Mar 2021
Edited: Eli Chen on 12 Mar 2021
Hi,
Assume you have the target image file at your working directory, for example, MatlabLogo.png. And you have a push button in your app. These steps can help you opening the image by clicking on the push button:
  1. Navigate to Component Browser, Select the push button in app tree, then define button clicking callback function name
2. Click the blue icon next to function name, you will be navigated to code view
3. Inside push button clicking callback function, you can use syntax like this:
% Button pushed function: Button
function showImage(app, event)
img = imread('MatlabLogo.png');
image(img);
end
When you run your app, and click this button, the image should be opened in Matlab Figure without browsing it

3 Comments

Hi @Eli Chen, Thanks for your response. I implemented the logic you have mentioned but I am getting following error.
If I still try to run the app, I am getting following error.
The error message says something is wrong at Line 202 in your app code, can you show me what code at Line 202?
HI @Eli Chen, Adding these lines of code to the app introduces error in line 202, otherwise there is no error in the app. before adding the lines of code, model was running fine.

Sign in to comment.

More Answers (1)

Hi @Eli Chen Please see thefollowing snapshot of code, The line where error lies comes in a non- editable part.

9 Comments

I saw you have 2 'end' after push button call back function. Can you please check if the last 'end' was valid?
Hi @Eli Chen I checked and removed that extra 'end'.
But I am getting following errorn in next code,
I saw you might have another invalid 'end' above the error line?
Hi @Eli Chen I see an extra 'end' in some other part of code. But I dont think that is making any problem, As the app is working fine before adding the code for 'Image pop-up' part in t'he app. That extra 'end' is present in the app since long and the app is working fine for those features..
Ok. It looks like the 'end' is not causing problem. Can you revert your changes(adding those end back) and try to comment out the pushbutton callback content? Like:
function showImage(app, event)
% img = imread('MatlabLogo.png');
% image(img);
end
If this still causes 'Unexpected Matlab Expression' error, then the problem could be you have created the pushbutton click callback funtion in wrong way
@Eli Chen No. by commenting out the code lines, there is no 'Unexpected Matlab expression' error. It debugs th code, and If I click Continue, then the app GUI Opens.
But if I just remove the '%' marks from the code, again the error appears.
Is it possible that you upload your app file and let me help debug with?
Unfortunately , As this work is confidential customer data, I wont be able to share app file.
As the error line mentiones, should I add UIAxes property in the app and some extra lines to the callback function ?
@Eli Chen I've tried your code, and it works. I'm trying to do something similar; however, I would like the image displayed in two different image holders on the same window as the pushbutton rather than in a pop-up window.

Sign in to comment.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products

Release

R2017a

Asked:

on 11 Mar 2021

Edited:

on 20 Dec 2021

Community Treasure Hunt

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

Start Hunting!