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