Replacing edited Data with New Data to Save to Text File in Appdesigner

1 次查看(过去 30 天)
I upload a textfile with data and want to repalce the old data with new data, but I end up having both the old data and my new data.
How do I only save the new data?
function InputDataButtonPushed(app, event)
[filename, path] = uigetfile('*.txt');
figure(app.UIFigure);
app.T = readtable(filename, 'Delimiter', 'space');
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));
function EditButtonPushed(app, event)
app.T.ct(1) = app.ctEditField.Value;
app.T.cr(1) = app.crEditField.Value;
function SaveButtonPushed(app, event)
Q = table2array(app.T(:,:));
fileid = fopen('filepath.txt','w');
fprintf(fileid, '%6.5f ', (Q));
fclose('all');
My output file shows the row of data for my old data and my new data like this:
0.09000 0.45000 2.00000 2.00000
Where the 2's are my new data.

采纳的回答

Voss
Voss 2022-4-9
My guess is that "ct" and "cr" are not the names of the columns/variables that readtable returns after reading the text file:
% make a file with column/variable names "a" and "b"
[fid,msg] = fopen('test_table.txt','w');
fprintf(fid,'a b\n0.09 0.45\n');
fclose(fid);
% read the file into a table T
T = readtable('test_table.txt','Delimiter','space')
T = 1×2 table
a b ____ ____ 0.09 0.45
% setting ct and cr in the table adds new columns/variables
T.ct(1) = 2;
T.cr(1) = 2;
T
T = 1×4 table
a b ct cr ____ ____ __ __ 0.09 0.45 2 2
If the text file did contain variable names "ct" and "cr", the new values from the EditFields would replace the values in the table:
% now make a file where the columns/variables are "ct" and "cr":
[fid,msg] = fopen('test_table.txt','w');
fprintf(fid,'ct cr\n0.09 0.45\n');
fclose(fid);
% read the file into a table T
T = readtable('test_table.txt','Delimiter','space')
T = 1×2 table
ct cr ____ ____ 0.09 0.45
% this time, setting ct and cr replaces what's in the table
T.ct(1) = 2;
T.cr(1) = 2;
T
T = 1×2 table
ct cr __ __ 2 2
If this is in fact the cause of the problem, you can fix it by ignoring the column/variable names when you read the file and then naming them "ct" and "cr" afterward:
% make a file with column/variable names "a" and "b" again
[fid,msg] = fopen('test_table.txt','w');
fprintf(fid,'a b\n0.09 0.45\n');
fclose(fid);
% read the file into a table T, this time telling readtable to ignore the
% column/variable names in the file (which will use names "Var1" and "Var2")
T = readtable('test_table.txt','ReadVariableNames',false)
T = 1×2 table
Var1 Var2 ____ ____ 0.09 0.45
% then rename "Var1" and "Var2" to "ct" and "cr"
T.Properties.VariableNames{'Var1'} = 'ct';
T.Properties.VariableNames{'Var2'} = 'cr';
% now setting ct and cr replaces what's in the table
T.ct(1) = 2;
T.cr(1) = 2;
T
T = 1×2 table
ct cr __ __ 2 2
Of course, without having a sample text file where this problem happens, I can't say for sure what's going on. If you can't get it to work, upload a sample file we can use to test with.
Also, hard-coding the column/variable names suggests that readtable may not be the best way to read your files. Maybe readmatrix would be easier to work with.
  10 个评论
I
I 2022-4-18
It says NaN and I really do not understand why. I don't see any syntax errors either.
Is there an issue with using readtable? Or do you believe that I must use readmatrix?
I
I 2022-4-20
I am just going back to using readtable and I will stick to the default variable names within the code since that essentially fixes the entire problem at the moment. At the moment, I am just trying one input file. I am not sure how the default variable names will change when I use more than one input file within the same code. At least it works for now with the one input file.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by