MATLAB Answers

Help!!! How to browse text file data to another function

1 view (last 30 days)
Jomei
Jomei on 8 Mar 2020
Commented: Jomei on 9 Mar 2020
Hi, I'd like to creat an GUI, and it works fine in browsing text file but I can't get the data in another function.
This is a pushbutton to select the file, and Unexcited_Sample_file is the file data (that is a 240 * 5 double matrix).
function Unexcited_Sample_button_Callback(hObject, eventdata, handles)
[Unexcited__Sample] = uigetfile ('*.txt');
Unexcited_Sample_file = load(Unexcited__Sample); % read (load) the file which is a 240 * 5 double matrix
assignin('base','Unexcited_Sample_file',Unexcited_Sample_file);
However, I'd like to get Unexcited_Sample_file data in the function Plotting_button_Callback, it becomes 0.
Is that because I use wrong pointer?
function Plotting_button_Callback(hObject, eventdata, handles)
Unexcited_Sample_file = get(handles.Unexcited_Sample_button,'value'); % it becomes 0!!!
How can I improve this code?
Thanks

  0 Comments

Sign in to comment.

Answers (1)

Benjamin Großmann
Benjamin Großmann on 9 Mar 2020
It seems that you access the value of the button, which is not what you intend to do. What you probably want to do is to share data among callbacks: https://de.mathworks.com/help/matlab/creating_guis/share-data-among-callbacks.html
In your two functions you could add something like
data = guidata(hObject);
to retrive the data and
guidata(hObject, data)
to store data. The guidata can only exist of a single variable, but this variable could be a struct which stores different data as fields.
You can add the following to the button callback to store the sample data
data = guidata(hObject);
data.Unexcited_Sample_file = Unexcited_Sample_file;
guidata(hObject, data)
And the following to the plotting function
data = guidata(hObject);
Unexcited_Sample_file = data.Unexcited_Sample_file;
Please also consider to initialize the struct variable (e.g. in gui opening fcn)

  1 Comment

Sign in to comment.

Sign in to answer this question.


Translated by