フィルターのクリア

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 Center および File ExchangeText Data Preparation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by