Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

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

1 vue (au cours des 30 derniers jours)
Jomei
Jomei le 8 Mar 2020
Clôturé : MATLAB Answer Bot le 20 Août 2021
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

Réponses (1)

Benjamin Großmann
Benjamin Großmann le 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 commentaire
Jomei
Jomei le 9 Mar 2020
It works, thanks alot!!!

Community Treasure Hunt

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

Start Hunting!

Translated by