필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
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.

답변 (1개)

chicken vector
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.
See an example here.

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by