Only Save the New Numerical Data to Text File

조회 수: 1 (최근 30일)
I
I 2022년 4월 5일
댓글: I 2022년 4월 7일
I want to save the newly edited data to the same text file after clicking the save button.
I have 10 variables.
This is what I have for my edit data:
function EditButtonPushed(app, event)
app.T.ct(1) = app.ctEditField.Value;
app.T.cr(1) = app.crEditField.Value;
When saving the data:
Q = table2array(app.T(1, :));
fileid = fopen('filename.txt','w');
fprintf(fileid, '%6.5f ', (Q));
The textfile output is:
0.09000 0.45000 45.00000 0.00000 0.00600 0.24200 4.67000 4.00000 0.89925 1.00000 2.00000 2.00000 2.00000 2.00000 2.00000 2.00000 2.00000 2.00000 2.00000 2.00000
I edited each number to 2 and for some reason, my old data appears in front of my new data.
How do I only save the newly edited data?

답변 (1개)

Jan
Jan 2022년 4월 5일
If you do not close a file opened by fopen with fclose is stays open.
Solution: Append an fclose(fileid) after the fprintf.
  댓글 수: 3
Jan
Jan 2022년 4월 6일
Is the file still open? Try to close all open files:
fclose('all')
If you are still appending the data, you either open the file with 'a' instead of 'w', or the data are included in your array Q, although you expect something else.
I
I 2022년 4월 7일
I fried fclose('all') as well. I am assuming that since the initial data is still showing up, then the initial data must be in the array for some reason. This at least helps me know where to look. Here is how I am importing and editing each piece of data:
For the open button:
[filename, path] = uigetfile('*.txt');
figure(app.UIFigure);
app.T = readtable(filename, 'Delimiter', 'space');
And then this is followed by each variable, but I'll just show 2 to be less redundant.
app.ct = app.T(1,1);
app.cr = app.T(1,2);
app.ctEditField.Value = table2array(app.ct(1,1));
app.crEditField.Value = table2array(app.cr(1,1));
The edit button:
app.T.ct(1) = app.ctEditField.Value;
app.T.cr(1) = app.crEditField.Value;
app.ctEditField.Value = app.T.ct(1);
app.crEditField.Value = app.T.cr(1);
I am failing to just replace the new value with the old value somewhere in here?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by