Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Gui loading a .mat file

Asked by Melvin on 25 Feb 2012

Hi everyone. My question is that, what should i put under a callback function of a pop up menu for example, when I want to load a .mat file. So that .mat file will be loaded to the workspace. Also if i will click to another case of that menu, what code should I use to clear up that workspace in matlab? Thank you very much...

1 Comment

Melvin on 25 Feb 2012

I hope someone will answer my question...Thank you again...

Melvin

Products

No products are associated with this question.

1 Answer

Answer by Image Analyst on 25 Feb 2012

Here's a snippet for you:

% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\Program Files\MATLAB';
if ~exist(startingFolder, 'dir')
	% If that folder doesn't exist, just start in the current folder.
	startingFolder = pwd;
end
% Get the name of the mat file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.mat');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a mat file');
if baseFileName == 0
	% User clicked the Cancel button.
	return;
end
fullFileName = fullfile(folder, baseFileName)
storedStructure = load(fullFileName);

Each function has its own workspace. Clicking on another menu item will clear the old function's workspace - no need to have any code in there to do that. In fact, you have to do special things if you want the other function2 to see function1's workspace. See the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.3F

3 Comments

Melvin on 25 Feb 2012

Thank you.I have another question. How can I plot in xy plane that .mat file? for example, that .mat file contains x and y variables with same size like n by 1 each?

Image Analyst on 25 Feb 2012

You mean like this
x = storedStructure.x;
y = storedStructure.y;
plot(x, y, 'rs-', 'LineWidth', 2);

Melvin on 25 Feb 2012

yeah...thank you very much...

Image Analyst

Contact us