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);
Related
Can I execute script, but make it doesn't touch any current workspace variables? I.e. turn script into function?
Also it would be good to have access this script's variables after run.
I tried
evalin('myworkspace', 'myscript')
but it failed.
I tried
evalin('caller', 'myscript')
but it changed variables.
Is it possible to accomplish?
The trivial way to accomplish this without changing the script itself would be to:
save
myscript
% ... examine variables
clear
load
save saves the current workspace to a MAT-file called "matlab.mat". You can give it a different name if you prefer. load loads it in again. If you specified a different name for save, give the same name to load.
If you have the parallel computing toolbox, you can do it by submitting the script to your local cluster. (Just to emphasise, your local cluster is your own pc.)
E.g. if you have a script SO.m with the line
a = randn;
then you can submit and wait using
job = batch('SO', 'Profile', 'local');
wait(job);
Once it have finished running, you can load the variables using
M = load(job);
then all the variables are fields of M.
>> M.a
ans =
0.4010
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())
I currently have a GUI that outputs a single text file based on various input parameters. However, I need to modify my application such that the GUI outputs multiple text files based on N inputs. The original GUI designer is no longer available and the main m-file has over 5k lines of code making it difficult to troubleshoot (not to mention the code is very unorganized and not commented). Does anyone have any suggestions on how I can run the GUI N times based on the N inputs and output N text files without modifying the original m-file?
Assume your gui is called myApp and your callback to s called myButton_Callback.
I also assumng that the tag of the uicontrol is 'myButton'.
Here is the caller script:
myApp_h = myApp();
myApp_handles = guidata(myApp_h);
myButton_h = myApp_handles.myButton;
MyApp('myButton_Callback', myButton_h, myApp_handles);
You can automate any gui control by this method.
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
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.