How to save and later clear the data taken through editText in a .txt file automatically with the current timestamp ?
2 次查看(过去 30 天)
显示 更早的评论
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1363813/image.jpeg)
Hi, I am going to enter the text fields in these editTexr's respectively. I want a button that can store them into a destination .txt file, with the current time(stamp) and clear the data. Please help with the callbacks.
0 个评论
回答(1 个)
chicken vector
2023-4-22
编辑:chicken vector
2023-4-22
You can use the Children property of your (ui)figure to collect every TextEdit object and access the String inside.
You can then procede to write the data in a .txt and save the time with datetime.
uif = uifigure('Position', [100 100 400 700]);
layout = uigridlayout(uif, ...
'ColumnWidth', {'1x','4x','1x'}, ...
'RowHeight', {'1x','1x','1x','1x','1x','1x','1x','1x','1x'});
edit1 = uieditfield(layout, ...
'HorizontalAlignment', 'center', ...
'Value', 'This is text 1');
edit2 = uieditfield(layout, ...
'HorizontalAlignment', 'center', ...
'Value', 'This is text 2');
edit3 = uieditfield(layout, ...
'HorizontalAlignment', 'center', ...
'Value', 'This is text 3');
button = uibutton(layout, ...
'Text', 'Store', ...
'ButtonPushedFcn', @(src,event) store(uif));
edit1.Layout.Row = 2;
edit1.Layout.Column = 2;
edit2.Layout.Row = 4;
edit2.Layout.Column = 2;
edit3.Layout.Row = 6;
edit3.Layout.Column = 2;
button.Layout.Row = 8;
button.Layout.Column = 2;
function store(uif)
% Find editfield objects:
editfieldObj = findobj(uif.Children.Children,'Type','uieditfield');
% ^ ^ ^
% figure grid fields and button
% Current date:
now = char(datetime);
% lines to be saved:
lines = {now, editfieldObj(:).Value, ''}';
% Save to .txt:
writecell(lines,'filename.txt')
end
If you want to append multiple data to the same file use fopen instead.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!