FDS and Matlab: Reading and writing an array in a file text

7 ビュー (過去 30 日間)
María Plata
María Plata 2020 年 1 月 28 日
回答済み: María Plata 2020 年 1 月 29 日
Good morning.
I have a problem with one script I am creating. The problem comes once I need to rewrite the sentence that is in my text file in order to change the number 330 with another number:
&RAMP ID = 'rampM', T=330, F = 1/
The main problem is that when I rewrite all the sentence the apostrophe tells matlab that the end of the sentences is after the equals and matlab doesn't take all the line from the text file.
I would like to know how to only change the 330 in order to keep the rest of the sentece the same.
Best regards.
Maria.
  2 件のコメント
Guillaume
Guillaume 2020 年 1 月 28 日
The answer to your question depends entirely on how you are currently reading and writing the file. We probably need to see some code.
"matlab doesn't take all the line from the text file.". Low level IO functions read whatever you tell them to read, including a whole line if you want, but it's unclear if you're using low-level functions.
María Plata
María Plata 2020 年 1 月 28 日
I have two different codes in order to change the strain but anyone works.
The first one is:
fid = fopen('himoto_1a.txt','r');
f=fread(fid);
fclose(fid);
line=168;
C = textscan(fid,'%s',line);
str='&RAMP ID = 'ramp M' , T=330, F = 1/' %%%%% MY PROBLEM IS HERE because of ''
newStr = strrep(str,'330','40')
And the second code is:
replaceLine = 168;
numLines = 362;
newText = '&RAMP ID = 'ramp M' , T=40, F = 1'; %%%%% MY PROBLEM IS HERE because of ''
fileID = fopen('himoto_1a.txt','r');
mydata = cell(1, numLines);
for k = 1:numLines
mydata{k} = fgetl(fileID);
end
fclose(fileID);
mydata{replaceLine} = newText;
fileID = fopen('himoto_1a.txt','w');
fprintf(fileID,'%s\n',mydata{:});
fclose(fileID);

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2020 年 1 月 28 日
Oh, your problem is actually with constructing the replacement string, not the actual replacement in the file.
Yes, that's not how you compose text in matlab.
newtext = '&ramp ID = ''ramp M'', T=40, F = 1';
In a char vector, if you want insert actual ' (single quote) characters, you need to double them:
c = 'this a char vector with a '' quote';
You could also work with the newish (R2016) string data type instead of char vectors. In your case, it doesn't make a difference but generally string arrays are easier to work with.
newtext = "&ramp ID = 'ramp M', T=40, F = 1";
In a string, if you want to insert actual " (double quote) characters, you need to double them:
s = "this a string with a "" quote";
Note that your first piece of code can't possibly work. You're closing the file before attempting to read it with textscan. Overall, I'd recommend you use this:
filelines = splitlines(fileread('himoto_1a.txt')); %easiest way to get lines out of a file
filelines{168} = '&ramp ID = ''ramp M'', T=40, F = 1';
%unfortunately, there's no equivalent to fileread for writing file
fid = fopen('himoto_1a.txt','wt');
fprintf(fileID, strjoin(filelines, '\n')); %using strjoin avoids insert a line return at the end of the file
fclose(fid);

その他の回答 (1 件)

María Plata
María Plata 2020 年 1 月 29 日
Thank you very much for your help.
Regards.
Maria.

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by