how to load .mat file into gui with edit box

23 ビュー (過去 30 日間)
tyna
tyna 2019 年 4 月 2 日
編集済み: Cris LaPierre 2019 年 4 月 8 日
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.

採用された回答

Cris LaPierre
Cris LaPierre 2019 年 4 月 7 日
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 件のコメント
Cris LaPierre
Cris LaPierre 2019 年 4 月 7 日
編集済み: Cris LaPierre 2019 年 4 月 8 日
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)
tyna
tyna 2019 年 4 月 8 日
Now I understand. Thank you so much.

サインインしてコメントする。

その他の回答 (2 件)

Cris LaPierre
Cris LaPierre 2019 年 4 月 3 日
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 件のコメント
tyna
tyna 2019 年 4 月 3 日
Thanks, but still is not working.
Cris LaPierre
Cris LaPierre 2019 年 4 月 3 日
編集済み: Cris LaPierre 2019 年 4 月 3 日
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?

サインインしてコメントする。


tyna
tyna 2019 年 4 月 7 日
編集済み: tyna 2019 年 4 月 7 日
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.

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by