MATLAB Answers

MATLAB GUI: How do I pass image matrix into function

10 views (last 30 days)
William Pang
William Pang on 28 May 2020
Commented: Tommy on 29 May 2020
Hi,
So I'm trying to read the image and then pass it through a function so that I can perform image analysis with some defined parameters.
For the "Load Image" button here's the code:
% Button pushed function: LoadImageButton
function LoadImageButtonPushed(app, event)
[FileName, FilePath]= uigetfile('.TIF');
I=imread([FilePath FileName]);
imshow(I, 'Parent' , app.UIAxes); %Displays selected image onto original image
I would then need to define some paratmers (Filter Size, Threshold, Cut Cells Regions) before running my external function. Once you input the paramters and press "Compute", I'm hoping that it would go into my function that I've written (called trialfunction here):
function ComputeButtonPushed(app, event)
filter_size = app.FilterSizeEditField; %Input parameter
trialfunction(I_M,filter_size)
S = app.ValueEditField;
The "trial function" is just a simple range filter that manipulates the image (stored in a 2D matrix):
function[S]=trialfunction(I_M,filter_size);
I_bin_regions = rangefilt(I_M,ones(filter_size,filter_size));
S=sum(I_bin_regions,'all')
end
However, I'm having trouble linking the data from I (2D matrix) into my function. How can I "connect" the "Load Image" function with the "Compute Button" function?
Thanks so much!
Note: I have tried adding app.I.ImageSource = imread (...) but that doesn't work. Error message says "
Unrecognized method, property, or field 'I' for class 'imageprocessing'.

  2 Comments

Rik
Rik on 28 May 2020
Did you make sure I is a property before trying app.I=?
William Pang
William Pang on 28 May 2020
I'm a bit new to the GUI interface, so apologies if I'm asking stupid questions, but how would you make I a property? I know that the image is reading properly (since I'm able to display it without adding the "app.I", just as "I") but would appreciate any pointers on how to make "app.I" a property.

Sign in to comment.

Accepted Answer

Tommy
Tommy on 29 May 2020
Edited: Tommy on 29 May 2020
In appdesigner, first make sure you are in code view:
Then in the code browser on the left side, go to the tab labeled Properties and select the green plus sign. The property will be private by default. If you want the property to be public (accessible from outside the app), you can click the down arrow and choose Public Property.
(edit) If you are calling imshow and rangefilt on this I, then I would use
app.I = imread (...)
where app.I is an array, rather than
app.I.ImageSource = imread (...)
where app.I is a uiimage.

  6 Comments

Show 3 older comments
Tommy
Tommy on 29 May 2020
I believe you'd get that error message if you had
app.ValueEditField = s
You can't set app.ValueEditField, which has type matlab.ui.control.NumericEditField, to a double (i.e. a number). If you are hoping to set the edit field's value, you'd have to use
app.ValueEditField.Value = s;
Or did you use this line and still get that error?
William Pang
William Pang on 29 May 2020
Yup, I get a different error.
Using just "app.ValueEditField":
It's weird because I am definitely getting a numerical value in the command window (so I know the function is working and something is passing through it) but doesn't seem to talk with the App Designer.
Tommy
Tommy on 29 May 2020
Ah, there are two different types of edit fields in appdesigner, text and numeric. If you want to display numbers, your edit field needs to be numeric. Are you sure you chose the correct style when you created the user interface?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!