How to connect GUI sliders to image analysis
Show older comments
I want to instantiate this GUI

The project is the analysis of bee pollen.I first load the image by the left pushbutton.There is a warnig msg imcrop which croppes the image to the desired ROI.ANd then there is a Statistic Analysis.Because of the trash there is a second take by which you can select the Threshold (slider on the top) and also select which sizes aroud the mean Area to select(slider 2&3 below).Then by pushbutton(new Analysis )there is a retake with new Statistic analysis..My problem is i dont know how to connect the sliders and the new Analysis with the retake procedure.This is my GUI code.
EDIT: I've attached your code to your post rather than display it all here
Accepted Answer
More Answers (3)
Cris LaPierre
on 20 Dec 2018
1 vote
Sorry but it's not clear to me what you mean by "retake".
From what I understand, I would recommend moving your statistics code to a new function (you can create your own functions, in addition to callbacks). At the end of your callback function that loads the image (pushbutton1?) you can call the statistics function.
The benefit is that any other callback can also call the statistics function. So in your New Analysis callback (pushbutton2?), you would also just call the statistics function.
Any variables you need could be added to the handles structure to make them available.
3 Comments
DIMITRIOS THEODOROPOULOS
on 20 Dec 2018
Cris LaPierre
on 20 Dec 2018
Connect to what? What should they be doing that they are not?
I see that changing the sliders 1-3 updates the string in edit10, edit11 and edit12 (though I can only guess which slider/edit box each one is). You know how to set the value of a slider from other callbacks.
set(handles.slider1,'value',string2num(edit));
Use a 'get' instead to access the slider value when needed in your new analysis
slide_thresh=get(handles.slider1,'value');
The other option is to add your slider values to the handles structure. Just be sure to update the handles structure so the changes are saved.
handles.slide_thresh=get(hObject,'value');
set(handles.edit10,'string',num2str(slide_thresh));
% Update handles structure
guidata(hObject.handles);
Image Analyst
on 20 Dec 2018
Like Cris say, you can create a custom function, like "PerformStatistics" or something:
function results = PerformStatistics(handles)
% Now do stats and make results
You only need handles if you want to use it in the function, for example to read the value of a slider, to get what files the user clicked on in a listbox, to check if the user checked a checkbox, etc. You could also pass in the values if you want, for example:
function results = PerformStatistics(sliderValue, computeResiduals, showPlot)
Then, in the callback function of the slider, or a button, or a checkbox, or wherever you want, you can just call the function, for example:
sliderValue = handles.slide_thresh.Value;
computeResiduals = handles.chkResiduals.Value;
results = PerformStatistics(sliderValue, slide_thresh, handles.chkShowPlot.Value);
So in your slider callback, you might simply set up the text label for it (assuming you have one)
handles.txtSliderLabel.String = sprintf('Threshold = %.2f', handles.slide_thresh.Value);
and then call PerformStatistics. If you have a table (uigrid) control on your GUI then you can load your results into that if you want. Or you could call xlswrite() or whatever you want to do with the results. You don't even have to pass any results back to the calling routine if you take care of everything inside PerformStatistics(). I hope that explains things a little more for you.
DIMITRIOS THEODOROPOULOS
on 20 Dec 2018
0 votes
1 Comment
Cris LaPierre
on 20 Dec 2018
Do you realize there is no code in pushbutton3's callback?
Let's forget the gui for a second.
Can you provide a script that starts with the cropped image and runs the new analysis and statistics on it? If you can provide working code, it will be much easier for us to show you how to adapt that to a gui.
Also, as I said before, we have no idea what components go to what callbacks. Some we can guess, but you also need to provide that info.
DIMITRIOS THEODOROPOULOS
on 21 Dec 2018
Edited: Cris LaPierre
on 21 Dec 2018
0 votes
7 Comments
Image Analyst
on 21 Dec 2018
Please attach the .m file and .fig file with the paper clip icon.
Cris LaPierre
on 21 Dec 2018
Edited: Cris LaPierre
on 21 Dec 2018
The error is because you put a period instead of a comma on line 360. It should be
guidata(hObject, handles);
Be careful - I didn't give you working code. I only shared what lines need to be changed. Be careful to copy the various bits of code I posted to its corresponding callback function. Basically, add 'handles." to the variables that store the image erodedII and the slider values (thresh, slider_min and slider_max).
Once you have done that, you can access those variables in the pushbutton3 callback as I showed.
Here are some links that might be helpful to you:
- Debugging your code
- Store or retrieve UI data
- Sharing data between callbacks in Guide
- Writing callbacks in Guide
Cris LaPierre
on 21 Dec 2018
Your slider values should appear in the edit boxes as soon as you replace the period with a comma in the guidata function call.
Also note that your edit10 callback at least is using string2num. That is not a MATLAB function. Do you mean str2num? Code analyzer will recommend you use str2double. Make that correction and changing the value in the edit box will update the slider as well.
Here is how I would code slider1 and edit10 to communicate with each other
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
handles.thresh=get(hObject,'value');
set(handles.edit10,'string',num2str(handles.thresh));
guidata(hObject, handles);
function edit10_Callback(hObject, eventdata, handles)
edit=get(hObject,'string');
set(handles.slider1,'value',str2double(edit));
DIMITRIOS THEODOROPOULOS
on 21 Dec 2018
Edited: DIMITRIOS THEODOROPOULOS
on 21 Dec 2018
Cris LaPierre
on 21 Dec 2018
Edited: Cris LaPierre
on 21 Dec 2018
That's probably because the variables thresh, slider_min and slider_max haven't been assigned a value yet. One option is to run your slider functions in the OpeningFcn. Just make sure the slider startup values (assigned in the component properties in the fig file) are what you want to use as your default values.
slider1_Callback(handles.slider1, eventdata, handles)
slider2_Callback(handles.slider2, eventdata, handles)
slider3_Callback(handles.slider3, eventdata, handles)
The other option is to just assign values to your varials in the OpeningFcn. If you want to do it that way, just add the following code. Same tip about setting the default values applies.
handles.slide_thresh=get(hObject,'value');
handles.slide_min=get(hObject,'value');
handles.slide_max=get(hObject,'value');
DIMITRIOS THEODOROPOULOS
on 21 Dec 2018
DIMITRIOS THEODOROPOULOS
on 22 Dec 2018
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
