How to Edit Imported Notepad Values and Save the New Values
조회 수: 3 (최근 30일)
이전 댓글 표시
I want to import a row of data that looks like:
0.09 0.45 45 0 0.006 0.242 4.67 4 0.89925 1
Set them equal to variables in my GUI
Edit the variables
Save the new values over the old ones
Here is what I have:
[filename, path] = uigetfile('*.txt');
figure(app.UIFigure);
app.T = readtable(filename, 'Delimiter', 'space');
ct = app.T(:,1);
cr = app.T(:,2);
app.ctEditField.Value = table2array(ct);
app.crEditField.Value = table2array(cr);
(excluded the other 8 variable since it seems redunant)
Each value edit field has the same function
value = app.ctEditField.Value;
To edit the variables, I did this:
app.T.ct(1) = app.ctEditField.Value;
app.T.cr(1) = app.crEditField.Value;
Then I save them:
Q = table2array(app.T(:,:));
fileid = fopen('name.txt','w');
fprintf(fileid, '%6.5f\n' , Q);
This results in the following output in my text file where I am only changing the value for ct to 5. The original data is on top of the new data in a column. I do not have an error message to debug. How do I properly edit/save the data to only include the updated data as a row of numbers rather than a column of numbers?
0.09000
0.45000
45.00000
0.00000
0.00600
0.24200
4.67000
4.00000
0.89925
1.00000
5.00000
0.45000
45.00000
0.00000
0.00600
0.24200
4.67000
4.00000
0.89925
1.00000
댓글 수: 0
답변 (1개)
Hornett
2023년 9월 29일
I understand you are facing problem while updating the already existing values in text file.
You can try using the "writematrix" function to write output data to text file.
Here is the sample code for “writematrix” usage:
M = magic(5)
writematrix(M,'mytxt.txt','Delimiter',' ')
For more detail you can refer to the following documentation page on writematrix function: https://www.mathworks.com/help/matlab/ref/writematrix.html
I hope this information resolves your query.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!