How to read strings and numbers in a text file?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ravindu Lokuliyana
am 15 Nov. 2018
Kommentiert: Ravindu Lokuliyana
am 19 Nov. 2018
Hi there,
I got an attached type text file which needs to read the content, set them into variables and rewrite the text file again.
E.g. read 'A' in the text file as a variable and change its value into '2' and rewrite the text file. Same procedure should conduct to B,C,Trail01 and Trial02.
Can anyone help me to solve the problem using this example file?
2 Kommentare
jonas
am 15 Nov. 2018
You should probably use regexprep, but I don't understand what you are trying to do. Can you upload a "solution" that you've formatted manually?
Akzeptierte Antwort
Jan
am 16 Nov. 2018
Bearbeitet: Jan
am 16 Nov. 2018
% Import the file:
key = 'SET LEVEL';
new = 5.0;
file = 'Test01.txt'
cstr = strsplit(fileread(file), '\n');
% Find and replace the wanted line:
match = strncmpi(cstr, key, length(key));
cstr{match} = sprintf('%s %g', key, new);
% Write the file:
[fid, msg] = fopen(file, 'w');
if fid == -1, error('%s', msg); end
fprintf(fid, '%s\n', cstr{:});
fclose(fid);
The part "Same procedure should conduct to B,C,Trail01 and Trial02" is not clear to me, but it should not be hard to modify the above code to your needs.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Text Data Preparation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!