Info

この質問は閉じられています。 編集または回答するには再度開いてください。

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

1 回表示 (過去 30 日間)
Jomei
Jomei 2020 年 3 月 8 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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

回答 (1 件)

Benjamin Großmann
Benjamin Großmann 2020 年 3 月 9 日
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 件のコメント
Jomei
Jomei 2020 年 3 月 9 日
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