Filter löschen
Filter löschen

How to save and later clear the data taken through editText in a .txt file automatically with the current timestamp ?

2 Ansichten (letzte 30 Tage)
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.

Antworten (1)

chicken vector
chicken vector am 22 Apr. 2023
Bearbeitet: chicken vector am 22 Apr. 2023
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.
See an example here.

Community Treasure Hunt

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

Start Hunting!

Translated by