MATLAB Answers

tyna
0

how to load .mat file into gui with edit box

Asked by tyna
on 2 Apr 2019
Latest activity Edited by Cris LaPierre
on 8 Apr 2019
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

Sign in to comment.

3 Answers

Answer by Cris LaPierre
on 7 Apr 2019
 Accepted Answer

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

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)
Now I understand. Thank you so much.

Sign in to comment.


Answer by 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

Thanks, but still is not working.
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?

Sign in to comment.


Answer by tyna
on 7 Apr 2019
Edited by tyna
on 7 Apr 2019

Yeah, sorry. My file is named gas, type double and has dimension 1826x2.
Everything is fine with file upload.
But when a variable is to be passed to load columns and display a graph, it won't happen.
Matlab writes this
Index exceeds matrix dimensions.
Error in hlavnigui>pushbutton2_Callback (line 92)
x = data(1:200,2);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in hlavnigui (line 43)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)hlavnigui('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
I even wrote it by hand to see a mistake.
case('nacteni')
data=(get(findobj('Tag','zadejnazev'),'String'));
load(strcat(data,'.mat'));
x = data(:,2);
y = data(:,1);
plot(x,y);
And it is basically same problem.
Error using plot
Invalid first data argument.
Error in fce (line 64)
plot(x,y) ;
Error while evaluating UIControl Callback.
So I need to figure out how to load individual columns. The file itself is loaded, but the columns are not. Thank you for trying to help me. I appreciate it.

  0 Comments

Sign in to comment.