how to load .mat file into gui with edit box
22 views (last 30 days)
Show older comments
Hi guys,
I have a problem. I have a GUI with edit box and pushbutton1(load data) and pushbutton2(plot data). i need to take the value from edit box it is the file name and then load it and plot it.
This is not working. I donť know, how can i do it. And I don´t know, what to set as a global variable so I can plot data and
where to place it in the code
function pushbutton1_Callback(hObject, eventdata, handles)
data = char(get(handles.edit1,'String'));
load(strcat(data,'*.mat'));
function pushbutton2_Callback(hObject, eventdata, handles)
x = data(1:200,2);
y = data(1:200,1);
plot(x,y,'x');
Thanks for help.
0 Comments
Accepted Answer
Cris LaPierre
on 7 Apr 2019
The file name isn't important since you capture that in a variable. What is the name of the variable in your mat file?
When you load a mat file, it loads the variables into the workspace. You need to refer to them by the name they already have. You have
data = 'gas';
load(strcat(data,'.mat'));
x = data(1:200,2);
y = data(1:200,1);
My suspicion is you are using the wrong variable name to create x and y.
Try this in MATLAB (not the gui) after making your current folder the one that has gas.mat in it.
clear
load gas
x = data(1:200,2);
I bet you still get the error. Now look in your workspace and see what variables exist. What is the name of the variable showing in your workspace that has your x and y data? That is the name you want to use.
2 Comments
Cris LaPierre
on 7 Apr 2019
Edited: Cris LaPierre
on 8 Apr 2019
Or perhaps this will help explain:
% create a variable and save it in a mat file named gas.mat
var = rand(500,2);
save gas var
% Clear the workspace - no variables exist.
clear
% recreate gui code
data = 'gas';
load(data)
% Now variable var is loaded to the workspace
x = var(1:200,2);
y = var(1:200,1);
plot(x,y)
More Answers (2)
Cris LaPierre
on 3 Apr 2019
You can accomplish this withough having to use global variables. What is typically done is the variable is added to the handles structure, and the guidata function is used to updata the handles structure in guide. See this example.
Keep in mind variable scope. Callbacks are functions, and once they are finished executing, its workspace and all variables created in it are cleared. With your sample code, I'd do something like this.
function pushbutton1_Callback(hObject, eventdata, handles)
handles.data = get(handles.edit1,'String');
% Update handles structure
guidata(hObject, handles);
function pushbutton2_Callback(hObject, eventdata, handles)
load(strcat(handles.data,'.mat'));
x = data(1:200,2);
y = data(1:200,1);
plot(x,y,'x');
Another option is to put all the code into pushbutton2
function pushbutton2_Callback(hObject, eventdata, handles)
data = get(handles.edit1,'String');
load(strcat(data,'.mat'));
x = data(1:200,2);
y = data(1:200,1);
plot(x,y,'x');
Note that I've updated a couple of your commands. First, no need to convert a string to a char in your get command for edit1. Second, I removed the '*' from .mat in your strcat command. That concatenates the * as part of the file name.
2 Comments
Cris LaPierre
on 3 Apr 2019
Edited: Cris LaPierre
on 3 Apr 2019
Can you share more? The code I shared worked in a gui I created with a mat file named data.mat that contained a variable data. Confusing, but that's what your code was expecting.
What error message are you getting? What isn't working? What are the variable names are loaded from the mat file?
See Also
Categories
Find more on Workspace Variables and MAT-Files 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!