I have a .dat file like in the picture and I would like to modify I3 values (the last column). I can't use textscan since the beginning of the file has a different format.
There are many methods to read files besides textscan. A simple google search should have turned up several.
What release are you using? Do you want to write the modified data exactly as it is in the original file? Then it might make the most sense to read as text and modify the char array directly.
I am using Matlab 2019b. Yeah I want to keep everything exactely the same except the last column where I want to replace the values by (old values+Delta(m,n)) .
If you are using R2017a or later you will see an 'Add-Ons' button in the lint if click on the 'HOME' tab. If you click on it you will see a window with a search box. Type readfile and hit enter. My submission should be one of the top results. Click on it (the name or the icon; both will work). Now will see the button 'Add from GitHub'. Once you click it and wait a few seconds you will see a green banner over the icon saying 'Installed'. That means you can use the function.
If you have R2016b or earlier you will have to get it manually. Go to the link I provided in my answer. There you can click the button 'Download from GitHub'. That will download a zip file. Somewhere in that zip file you will find readfile.m. Put that file in your current folder, or put it somewhere on you path (e.g. create a folder C:\FEX-submissions\ put readfile.m there and run addpath('C:\FEX-sumissions\','-end');).
Tell me which one of the two options you tried and what if anything went wrong when getting readfile. You can use the second option for newer releases as well if you prefer.
Sorry to bother you again I have another problem here.
Actually for the same m and n, The correction is the same so the size of my correction list is inferior than the number of lines in the text file (without headerlines). For example, as you can see in the picture, the first 5 lines that start with m = 0 and n = 1 have only one corresponding correction that should be added to all the five values in the last column of the text file.
I ve tried to convert the data{n} to num and then compare the two first values but when I go back to str after the modification, i don't find exactely the same format (there is a lot of spaces) Do you have any idea on how I can do this ?
How did you try to determine the values of m and n on a single line? That is your goal. Don't modify anything elso on the line, make a copy if you need to. Also note that m has values of 0, so those aren't valid Matlab indices.
I think you didn't get my request. Actually, what I asked for in the beggining was not what I wanted, your solution works perfect according to what I asked at the beggining. But now, I want to modify the alogithm so that for the same m and n, the addes value to the last column is the same like for example :
I did understand you. You just need to find a way to extract the values of m and n from a single line and use those to find the value you want to add. So how did you try to find m and n? Are m and n guaranteed to be single digit integers? That would make it a lot easier, although you could also just use textscan on the single lines by this point.
Who taught you str2num? All the way back to at least R2011a you can read the advice to choose str2double instead. I don't think there is only one way to go, but str2num is certainly a worse choice than textscan.
You should really read the documentation. It is one of the major benefits of Matlab. As for this specific problem: you can easily get the values for m and n:
I would separate finding the value of the correction and using it. I also would not use i and j as variables. You might also consider using ismember and/or find. A possible optimization would be to convert the mcorrection and ncorrection arrays to double only once (so outside the loop).
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
3 Comments
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/588496-modify-a-text-file#comment_995440
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/588496-modify-a-text-file#comment_995440
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/588496-modify-a-text-file#comment_995470
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/588496-modify-a-text-file#comment_995470
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/588496-modify-a-text-file#comment_995506
Direct link to this comment
https://au.mathworks.com/matlabcentral/answers/588496-modify-a-text-file#comment_995506
Sign in to comment.