I want to create a MATLAB gui where I can open file explorer using a pushbutton and select a file for further processing. How can I do that?
Also I want to know how to assign .m function files to the pushbuttons. I tried putting functionname.m file in callback of the pushbutton. But it didn't work.
Please help me with both doubts.
You'll need to write a callback function to launch the file selection dialog (uigetfile)
set(hbutton, 'Callback', #mycallback)
function mycallback(src, evnt)
[fname, pname] = uigetfile();
filepath = fullfile(pname, fname);
% Do something with filepath
end
In general if you want to call any .m file from within a callback, you'll want to wrap the call to it in an anonymous function
set(hbutton, 'Callback', #(src,evnt)functionname())
Related
I have a .m file that contains this lines
%reading 2 images
image1=imread('pic1.tif');
image2=imread('pic2.tif');
% two varialbes
number_of_points = 100;
simpling = 30;
I want a simple gui with matlab that allow the user to :
select the 2 images by opening a pop-up window to explore the files on the pc.
choose a value for the two variables "number_of_points" and "simpling" using radio buttons 100,150 or 200 for the first and 0 or 30 for the second.
I created a gui with matlab but I can't find a way to add those functions.
this is what it looks like :
http://s9.postimg.org/k6ed9pni7/stack.png
How can I make the gui execute my .m file with those parameters ?
I'm a newbie so any help will be appreciated.
One solution is to turn your m-file into a function that takes the four parameters as input. Than you can call your function from your Lancer button callback.
The alternative is to assign the values of your edit boxes & radio buttons to a variable in the base workspace and run your m-file as a script from the button's callback. If you prefer this method, you can assign variables from the GUI's functions in the base workspace (where your script will run) via evalin('base','expression')
Here,'expression' would be something like sprintf('myPic1 = %s; myPic2 = %s; number_of_points = %d; simpling = %d;', handles.myEdit1.String, handles.myEdit2.String, handles.myRadios1.Value, handles.myRadios2.Value);
I am working on a GA code in MATLAB. When I execute the following syntax in the command window
function[opt,fopt,histf]=ga(n,fitnessfct,decodefct,selectfct,stopeval)
I get the following error
Error: Function definitions are not permitted in this context.
You must define your function in another M-file named by ga.
1- Create a new script, M-file where you can you use "Ctrl+N"
2- Declare your function writing:
function [opt, fopt, histf] = ga(n, fitnessfct, decodefct, selectfct, stopeval)
% // function statements
end
3- Save the function file and name it as ga
4- Make sure setting the path of current directory to your working directory.
That is it..
I have a GUI that displays plots, and it launches a "playlist" window. When I perform an action in the playlist window, is there a way to run a function in the launching window?
To be more clear, if I add files in the playlist window, I would like the first file in the list to be displayed in the launching window, but I would like to do this through a function in the launching window rather than passing the plot handle to the playlist window.
Thank you in advance for any assistance you can offer!
One of the easiest ways to do this, would be using findobj.
This looks through graphics objects, finding those that match the provided filter criteria.
As the number of existing figures should be relatively small, it should also be reasonably fast.
Assuming your launcher-figure has some name you can get the launcher figure-handle
e.g. via
launcherFig = findobj(0,'type','figure', 'name', <launcher-name>);
Or give your lauchner figure a Tag that you can search for:
% in your launcher-figure code:
launcherFig = figure('Tag', 'MyLauncher');
% and modify the search accordingly:
launcherFig = findobj(0, 'type', 'figure', 'Tag', 'MyLauncher');
And, for completeness, though I don't like them, you could use a global variable:
% in your launcher-figure code:
launcherFig = figure(...);
% store handle in the global variable:
global LauncherHandle;
LauncherHandle = launcherFig;
% no need for a search now anymore, just get the global variable:
global LauncherHandle
Does the save function in Matlab save the thing saved in the same project file?
I'm trying to save a vector as 'mat' file.
This is my code :
function facePts = getFacePts(faceFileName)
if(exist('faceFileName','file')==2)
facePts=load('faceFilename.mat');
return;
end
img=imread(faceFileName,'tif');
showImage(img);
[x,y]=ginput(3);
facePts=[x,y]';
facePts=facePts(:);
save faceFileName.m, facePts; %%%%% HERE
end
The function compiles but I can't find the file I saved
I guess you want to do this:
save('faceFileName.mat', 'facePts');
Ok, so I figuered out that the path of the current folder wasn't the path of my Project.
I changed that by going to 'Desktop' in the Bar, checked 'Current Folder' and chaged the path there.
Now it works !
I have two GUIs namesd masir and SetOut
SetOut GUI is a sub GUI for masir(pressing a button on masir will open SetOut)
To access the data of masir in SetOut I have these 2 lines of code:
masirGUIhandle = masir;
masirGUIdata = guidata(masirGUIhandle);
but running these 2 lines will run the opening function of masir as I work in SetOut(In opening function I have set some initial values for my variables and now I don't want those initial values ,I need changed values for my variables) so I dont want the OpeningFcn of masir GUI to be runned ,I just need to have access to masir data in SetOut
What can I do to fix the problem?
Can any one help me about this answer and explain me more?
I use this easy way for data sharing between GUIs
%In the end of OpeningFcn of Main GUI
setappdata(0,'HandleMainGUI',hObject);
%When you want to edit shared data you must get the handle
HandleMainGUI=getappdata(0,'HandleMainGUI');
%write a local variable called MyData to SharedData, any type of data
setappdata(HandleMainGUI,'SharedData',MyData);
%get SharedData and save it to a local variable called SomeDataShared
SomeDataShared=getappdata(HandleMainGUI,'SharedData');
Don't forget to clean up the data shared in the CloseReqFcn of you main GUI
HandleMainGUI=getappdata(0,'HandleMainGUI');
rmappdata(HandleMainGUI,'MySharedData') %do rmappdata for all data shared
Remember that your GUIs might try to getappdata that doesn't exist, you should first test if it does exist
if (isappdata(0,'HandleMainGUI') & isappdata(HandleMainGUI,'MySharedData'))
%get, set or rm appdata
else
%do something else, maybe loading default values into those variables
end
Tell me more aboute which line of code should be written in MainGUI and which line should be written in SubGUI?
And tell me what does the responser mean by CloseReqFcn?
Well let me summarize how I see the problem.
You want to read the data from SetOut with out creating it? That's not possible like this as the data will be created when the window is created.
A nice and systematic way around would be doing it object oriented (see Model-View Controller Pattern) You can more or less copy an example from my answer here (Example for Event - Observer)
But if you'd like to stick with your code I also have some ideas:
If you don't want the window to show you could set it invisible with set(theGUIhandle,'Visible','off')
While the window is not closed you can obtain the data with getappdata(theGUIhandle)
If you want the data after the window is closed you need to have a function that stores it outside the window.