Replacing edited Data with New Data to Save to Text File in Appdesigner
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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.
0 Commenti
Risposta accettata
Voss
il 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')
% setting ct and cr in the table adds new columns/variables
T.ct(1) = 2;
T.cr(1) = 2;
T
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')
% this time, setting ct and cr replaces what's in the table
T.ct(1) = 2;
T.cr(1) = 2;
T
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)
% 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
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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Function Creation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!