I am writing a GUI program with matlab and I would like to test my variables to see if they are properly imported and to test how to access to different parts of the variables imported so I want my variables to be shown in workspace window of Matlab desktop
the main trouble is if I write a common program in the mfile editor after running the program variables will be shown in workspace windows but about GUI programs its not true
And if I Save the workspace of my program from the path
File>Save Workspace As...
in MFile Editor and then I try to open this access file in desktop i encounter
No variables created
How can I accesss the workspace of my GUI?
If it is just for inspection, the easiest way is to use the debugger: set a breakpoint in one of the GUI callbacks, execution of the code halts there, and allows you to inspect the workspace, among other things.
If you want the GUI to return data to the main workspace, you add the line uiwait(hObject) to the end of the opening function. Then, the callback to e.g. the OK-button should have a line handles.Output=myData; guidata(hObject,handles); to send the variable myData to the GUI output, followed by uiresume(hObject). This way, you can call your GUI as myData = myGUI;, and myData in the base workspace will be filled with whatever data the OK-callback gives it once the OK-button is clicked.
Note: Functions assign outputs, not internal variables to the base workspace. So I guess what you describe as "functions in the editor" are actually scripts that access and modify the contents of the workspace from which they're called.
Related
I'm working on an App in app designer. Within the app the user will select a bunch of options before running some calculations.
To simplify this process I added a "Save as..." menu so that the user can save the current settings to a file (.mat) and reload them when they open the app the next time.
What I'm trying to achieve is that the user can double click on the previously saved .mat file, which will launch the app and the app will automatically read the double clicked file and load all the settings.
All this needs to happen after the app is compiled and distributed as an executable.
I'm thinking that one way to achieve this is to make a startup window of the app that calls the main window passing the file path as parameter.
Any suggestion would be really appreciated.
Hi, I think I may have a fairly simple, albeit involved, solution for you.
Brief solution overview (TL;DR)
Save the settings from the app with an extension other than .mat, e.g. .mydat. Add an App Input Argument and have the startupFcn treat the argument as a file name to a *.mydat file and be sure to also handle the case that the argument is left out. After the first output file is saved, use windows Open with... to select your app. Now double clicking the *.mydat file will open your app's .exe and will provide the file name of the clicked file to the input argument in your startupFcn.
An example in MATLAB 2018a as a compiled exe on windows 10.
Ok, to start. Let's setup a simple app (I called it runAppFromData) that takes a string input to an edit field and saves it in a file called 'settingsValues.mydat'. It looks like:
The callback for the Save button collects the Value into a local variable called value and then saves it to disk:
% Button pushed function: Save
function save(app, event)
value = app.InputField.Value;%#ok
% User selects save location
saveLocation = uigetdir();
% Now just save the value variable to the selected location.
save(fullfile(saveLocation,'settingsValues.mydat'), 'value', '-mat');
end
I don't know when appdesigner added the feature to "run app with inputs" but I have it with 2018a:
We make a single input, fileName that expects a file name as a string (you'll see why below). So add the input and click OK. Then we're sent to "code view" at the startupFcn. Here we'll write the logic that parses the input file. For my simple example app, I load the input file into a struct and then send the value to the edit field:
% Code that executes after component creation
function startupFcn(app, fileName)
if nargin < 2 % app is itself an argument
% just continue running the application without error
return
end
% fileName is a string, so let's load it into a struc
S = load(fileName, '-mat');
% The value field will be there because that is how we wrote it
app.InputField.Value = S.value;
end
Note, I performed a nargin check to handle the first-run case (and anytime the app is run from the actual executable).
MATLAB doesn't care what the file extension is of a matlab file and if you have an unknown file extension, e.g. .mydata, double-clicking the file in windows will ask you to choose the application, which works to your benefit for deployment:
A couple things to consider.
When the app is opened from the .exe it will always show the default values. If you want to input some other default values you can edit your windows shortcut Target field to supply a file path for the desired input file (see here). This saves recompiling with new defaults, but the file has to remain somewhere (you can package it with the app too).
Sorry this answer got soo long! I hope it helps!
You can't double click a .mat file and open an entire executable, but you can definitely add a startup function that asks you to open a .mat file. My suggestion though would be to make sure that you have a template file at least in place, so that the user doesn't run into problems the first time running the program where there is no file to open.
For example: Rstudio uses so called projects - text files ending with .Rproj. When you click on a project file, it opens up Rstudio and sets working dir to where the project file is. Optionally, it executes any code written it. However, it does not open itself (i.e., it does not show up in the script editor).
Is there something like that in Matlab? If not, how to emulate it?
Currently I use to make an .m file with cd, addpath calls etc. But when I click in the file browser:
it just opens Matlab and shows up in the script editor without running
opening Matlab is what I want, but showing up in the script editor is actually redundant; I only need to run it (and use the results in my Matlab desktop session)
(What I want to avoid is having to open the script file, run it manually and then having to close it again. It is annoying!)- edited
Matlab does not have "project files" (as far as I know).
However, I think you can easily emulate what you want.
Let's suppose you have your code in a folder C:\MyProject:
1) Create a new m-file C:\MyProject\MyProject.m with all your initialization code (cd, addpath calls, global variables, whatever you need).
Here's a simple example for demonstration purposes:
disp('Replace this with your initialization code');
2) Create a batch-file C:\MyProject\MyProject.bat as follows:
MATLAB -r "run MyProject"
Now, by double-clicking the batch-file you will:
open the complete Matlab environment
execute the script MyProject.m (without loading it in the script editor)
For this purpose, MATLAB offers startup.m files (online documentation).
You have to put all your initialization code in a file called startup.m, which needs to be located within the MATLAB search path (i.e. within your project folder). The script will be executed every time you open MATLAB by double-clicking a arbitrary m-file from your project folder.
Let's imagine that you are dividing your script by parts using functions. Every one has its own workspace. Is there any way to switch between workspaces and directly have a look at its content? It's just like we do with the base workspace.
If you only want to inspect different (read: parent) workspaces, you can do that while debugging using the "Function Call Stack" menu as shown below:
Each of the displayed lines (in this example: updateSourceImageAndStats, loadCallback, Base) represents a different workspace, and switching between them will show you the contents of the workspace for that level. Also consider dbstack and related functions.
If however you want to execute code in some parent workspace, you can use evalin.
I am trying to create a GUI for a script that receives paths and matrix names from the user (via uigetdir and uigetfile functions as well as edit text fields), and finally upon pushing the "Run" button writes these handles into a .m file and runs another script.
Essentially, in the end it should create a file called 'RunName'.m that looks something like:
base_path = get(handles.BasePathEdit,'String')
file_name = get(handles.FileNameEdit,'String')
main % runs the main script with the preceding variables defined as they were
and run it.
I'm guessing the script would run successfully if I just write it like that under the 'Run' push-button function, but I would still like for it to create a .m file as described.
Thank you very much for you help,
All the best.
The simplest way of accomplishing this is to write a function that executes upon the pressing of the 'run' button that reads the values from the GUI and the proceeds with the main script. In guide, this is simple - right-click on the 'run' button, and under 'View Callbacks', select 'Callback'. Then you can use the usual syntax get(handles.RELEVANT_TEXT_BOX,'string') etc to read the resr of the data in. You can then pass this to your main function, or you could then just copy and pase your script straight into the callback.
I am using matlab R2012b, with the eeglab plugin. This is a plugin with a GUI, while this is very helpful I want to be able to see what functions the plugin is running when I use the GUI is this possible?
If you enter the command EEG.history into the command window after your session with EEGLAB it will print out a list of the commands called for the session.
example ...
EEG.history
ans =
EEG = pop_fileio('D:\work\Matlab libraries\training_course_materials\Eeglab_data.set');
EEG.setname='temp_file';
EEG = eeg_checkset( EEG );
If you then select a function of interest, e.g. highlight pop_fileio above and then right click you can select 'Open selection' option from the popup menu and the file pop_fileio.m' will open in a new tab in the MATLAB editor for you to look at it.
It's worth noting that when you run EEGLAB as a GUI that a lot of subsequent function calls are made with default parameter setting however if you call them directly from your own code you can then change these default settings.
You can follow the execution of a program using debugger. It is very easy using matlab's editor. Alternatively you can use profiler to see what was exectured and how many times.
When you select an option on the GUI, a new form/window will open. The name of the fucntion will be displayed at the top of this new window. Locate where you have stored EEG lab and find the function pertaining to the name and you can view the details in your editor window.