GUI store the output data in matrix

4 views (last 30 days)
Jomei
Jomei on 13 Mar 2020
Answered: Aditya on 24 Jan 2025 at 6:29
Hi,
Now I have 2 output data and 1 button. When I press the button in the firsr time, 2 output data will store in first row. And when I press second times, It will store in second row, and so on.
function Plotting_button_Callback(hObject, eventdata, handles)
%output data
d1=str2num(get(handles.d1,'string'));
assignin('base','d1',d1);
ns=str2num(get(handles.ns,'string'));
assignin('base','ns',ns);
% store data
Store_data = [];
i=1;
Data_Number = str2num(get(handles.Data_Number,'string')); % data number test box
Data_Number = Data_Number + 1; % add 1 if pressing the button
set(handles.Data_Number,'string' , Data_Number)
assignin('base','Data_Number_updated',Data_Number);
Store_data(Data_Number,1) = d1;
Store_data(Data_Number,2) = ns;
assignin('base','Store_data',Store_data); % store data
And now, it's able to become a matrix. but the matrix just has correct value when last press, other time the output values repalce number to 0.
For example, I press this button 9 times and I didn't change anything, so the number should be all 1. However it becomes
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
1 1
--------------

Answers (1)

Aditya
Aditya on 24 Jan 2025 at 6:29
Hi Jomei,
The issue you're facing is due to reinitializing the 'Store_data' matrix as an empty matrix each time you press the button. This overwrites any previously stored data, leaving only the last entry.
To maintain the data across button presses using the base workspace, you can retrieve the existing 'Store_data' from the base workspace at the beginning of your callback function.
Here's how you can retrieve and update 'Store_data' correctly:
% Check if Store_data already exists in the base workspace
if evalin('base', 'exist(''Store_data'', ''var'')')
Store_data = evalin('base', 'Store_data');
else
Store_data = []; % Initialize if it doesn't exist
end
% code continue...
% In the end save the updated matrix back to the base workspace
assignin('base', 'Store_data', Store_data);
This approach ensures that 'Store_data' retains all previous entries and appends new data correctly each time the button is pressed.
I hope this helps!

Tags

Community Treasure Hunt

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

Start Hunting!