How to modify this code to make it import values from a file?

1 Ansicht (letzte 30 Tage)
Hi,
The following code opens a file (named "Pulse.acc") and writes c, s and t values as follows:
0 1 0
0 2 0
0 3 0
It also opens another file (named :Case.tcl") and writes each case number.
Each case over-writes the previous one.
I am now trying to import the values of s from the attached file (named "result_40.txt"). This file contains 40 columns and 10 rows. Each value in the columns will be printed in "Pulse.acc" file and excute OpenSees software model (along with c and t values. For example: 0 1 0, as shown above). This needs to continue until the 10th value in the column. Then, the second column starts.
The values of c and t will continue to be zero, so they could be left as below.
It would be great if a folder can be opened for the outputs from each column and numbered sequentially.
I hope I could explain what I want clearly, and please feel freel to let me know if I need to explain it further.
c=[0 0 0];
s=[1 2 3];
t=[0 0 0];
for j = 1:3
fidP = fopen('Pulse.acc','w+');
fidC= fopen('Case.tcl','w+');
fprintf(fidP, '%d\n%d\n%d',c(j), s(j), t(j));
fprintf(fidC, 'set case %d',j);
fclose(fidP);
fclose(fidC);
!OpenSees Model.tcl
end

Akzeptierte Antwort

Bob Thompson
Bob Thompson am 19 Feb. 2019
Importing and reading the data into matlab is fairly simple, and you can take your pick of text readers. It sounds like dlmread will work best for what you're trying to accomplish here so I will use that. Alternatively, you can investigate textscan(), csvread(), or just fopen() with fgetl().
fin = 'result_40.txt';
s = dlmread(fin,'');
c = 0;
t = 0;
for i = 1:size(s,2);
coldir = mkdir(['Column_',num2str(i)]);
cd(coldir);
for j = 1:size(s,1);
fidP = fopen('Pulse.acc','w+');
fidC= fopen('Case.tcl','w+');
fprintf(fidP, '%d\n%d\n%d',c, s(j,i), t);
fprintf(fidC, 'set case %d',j);
fclose(fidP);
fclose(fidC);
end
cd('../');
end
  24 Kommentare
Bob Thompson
Bob Thompson am 20 Feb. 2019
Ah, I see. That is because you're printing with the fprintf command as a double. Try the following:
fprintf(fidP, '%f\n%f\n%f',c(j), s(j), t(j));
You want to print the values as a 'floating point number' rather than a double in order to get the level of fidelity you're looking for. Sorry I didn't catch that earlier.
Ismail Qeshta
Ismail Qeshta am 20 Feb. 2019
I see. Thank you very much Bob. That is so kind of you. Alright, I will incorporate this correction in the code.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Low-Level File I/O 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!

Translated by