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

1 view (last 30 days)
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.

Accepted Answer

Voss
Voss on 9 Apr 2022
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 Comments
I
I on 18 Apr 2022
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 on 20 Apr 2022
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Function Creation in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!