Filter löschen
Filter löschen

Reading a text file with variables and parentheses between

4 Ansichten (letzte 30 Tage)
Mahdi Khademishamami
Mahdi Khademishamami am 5 Apr. 2023
Hello everyone,
I need that MATLAB read a text file in the format below, and seperate each variables with corresponding numerics, and write it back with the format of the text file again. I want to add another variable with the same number of numerical values and write it back to its initial format to be read again by my program.
species 1
species 2
species 3
(0.1
0.2
0
)
(.02
.1
.5
)
(0.1
.2
.3
)
Then, I want to add Species 4 with some numerical values in the same format as othe variables.
Thank you so much.

Antworten (1)

Walter Roberson
Walter Roberson am 5 Apr. 2023
S = fileread('YourFile.txt');
S = regexprep(S, '\)', '%g\n)');
newS = compose(S, ValuesForSpecies4);
fid = fopen('NewFile.txt', 'w');
fwrite(fid, newS);
fclose(fid);
My guess is that you will want to go slightly further and also insert the name of the 4th species. Possibly with something like
S = regexprep(S, '\(', 'name of species 4\n(', 'once');
  1 Kommentar
Mahdi Khademishamami
Mahdi Khademishamami am 5 Apr. 2023
Hi Walter,
Thank you so much for the answer. I inserted your script in MATLAB, and it give me S as a character with htis output
species 1species 2species 3(0.10.20%g)(.02.1.5%g)(0.1.2.3%g)
What I want is actually identify and seperate the variables like Species 1=[0.1,0.2,0], Species2=[0.02,.1,.5], species3=[0.1,0.2,0.3], then add species4=[0,0,0] and then put it back at the same format like below
species 1
species 2
species 3
species4
(0.1
0.2
0
)
(.02
.1
.5
)
(0.1
.2
.3
)
(0
0
0
)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Import and Export finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by