How to delete/remove last row from .txt file in a for loop?

Hello all,
I need your help for my class project. I need to add a for loop to my code and delete last row (time and displacement vectors) of the txt file for each iteration.Also, for each iteration I want to store my parameters and get plot. My aim is checking the time, is it long or enough? I've tried some codes and read previous questions but I couldn't fix my code.I'm really got stuck. If you're able to help,I'd be appreciate. Thanks

2 Comments

What does "delete last row of text file" mean? Do you really want to modifiy the file?
What does this mean: "for each iteration I want to store my parameters and get plot"? A plot of what? I do not understand this also: "checking the time, is it long or enough"? How is "enough" defined?
What is the problem with your code?
By the way, source code looks like 1980 with all variables in UPPERCASE.
Attached .txt file is result of specimen test. It has time and displacment values. I would like to check the test time (is it too long or not ), therefore in each iteration I want to delete last row/ time value, recalculate the model fit parameters and plot Model vs Time plot.
Meanwhile, I added below codes and able to delete last row, but couldn't create new "demodata.txt" file.
Thanks for response.
DAT(end, :) = [];
delete('demodata.txt');
fprintf('demodata.txt');

Sign in to comment.

 Accepted Answer

The description is vague for reasons outlined in Jan's comment.
  1. List path to all text files. See dir.
  2. Read in the text file. See readtable, readmatrix, readcell,readtimetable.
  3. Eliminate the last row using indexing. T(end,:)=[];
  4. To store values within a loop, see preallocation, and use indexing.
If you get stuck, circle back in for another comment and show us what you've got.

6 Comments

Many thanks for the help. I've updated the code but I could not create for loop, actually I couldn't create vectors. First, I've tried to add while loop for iteration. For each iteration code deleted the last row but didn't store the paramters. I couldn't create the vectors inside the loop to store the calculated parameters for each iteration. Could you please suggest me a solution? Thanks again
In the comment I find: "adding a for loop to your Matlab code that shortens the Time and Displacement vectors by one element for each iteration." This does not mean any deleting of lines in files on the disk. You should crop the last element of the Time and Disp vectors only.
This example stores a variable in a cell array
n = 10; % number of iterations
someVariable = cell(1,n);
for i = 1:n
someVariable{i} = someFunction(__);
end
For numeric scalar outputs,
n = 10; % number of iterations
someVariable = nan(1,n);
for i = 1:n
someVariable(i) = someFunction(__);
end
You're right, I don't need to create new .txt file.
I could't create a vector for my function handle -"Model" so couldn't run the for loop. Do you know how can I do this? Thanks
Are you asking how to design the objective function for lsqnonlin? There are examples in the documentation.
And here's info on passing additional values
Or are you asking how to preallocate a loop variable for storing the model outputs?
I'm asking how to preallocate a loop variable for storing the model outputs. Thanks

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!