Info

此问题已关闭。 请重新打开它进行编辑或回答。

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

1 次查看(过去 30 天)
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
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)

Community Treasure Hunt

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

Start Hunting!

Translated by