how to load .mat file into gui with edit box
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.
Accepted Answer
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
tyna
on 3 Apr 2019
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?
Categories
Find more on Graphics 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!