reading text file in matlab
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I want to read the following in matlab.
Each row has 4 columns: 1 for time, and the other three columns corresponding to the 3 velocity components (within brackets) at 3 locations.
Time (U1x U1y U1z) (U2x U2y U2z) (U3x U3y U3z)
I want each value in a column matrix.
How can textread/dlmread be used to obtain the matrices? Any inputs are welcome!
sample data:
3.99968e-06 (2.05399e-13 -3.88282e-12 0) (5.24149e-11 -6.83701e-05 0) (3.34559e-13 -9.11144e-10 0)
7.99936e-06 (5.10739e-13 -1.17757e-11 0) (1.92955e-10 -9.22845e-05 0) (6.46889e-13 -1.60874e-09 0)
1.1999e-05 (8.09403e-13 -3.2686e-11 0) (4.97752e-10 -8.93228e-05 0) (8.33059e-13 -1.80362e-09 0)
1.59987e-05 (8.01e-13 -1.1137e-10 0) (9.14563e-10 -8.64096e-05 0) (8.4271e-13 -1.57685e-09 0)
1.99984e-05 (2.44508e-13 -4.59938e-10 0) (1.25501e-09 -8.80288e-05 0) (7.85553e-13 -1.18146e-09 0)
2.39981e-05 (-9.16588e-13 -1.97538e-09 0) (1.30702e-09 -9.00981e-05 0) (7.69317e-13 -8.37674e-10 0)
2.79978e-05 (-2.30847e-12 -7.86093e-09 0) (1.06111e-09 -9.10861e-05 0) (9.24744e-13 -6.81356e-10 0)
3.19974e-05 (-2.5133e-12 -2.76676e-08 0) (7.65202e-10 -9.15626e-05 0) (1.28747e-12 -7.55801e-10 0)
3.59971e-05 (5.19913e-13 -8.56329e-08 0) (7.20285e-10 -9.19932e-05 0) (1.84566e-12 -1.00681e-09 0)
3.99968e-05 (9.39936e-12 -2.34462e-07 0) (9.94357e-10 -9.2398e-05 0) (2.47688e-12 -1.30693e-09 0)
0 commentaires
Réponse acceptée
Walter Roberson
le 25 Août 2011
If you use textscan(), you can set Whitespace to include the '(' and ')' characters, and then it would be a plain format of repmat('%f',1,10)
Plus de réponses (1)
TAB
le 25 Août 2011
These are the steps 1. Read the text line by line 2. Remove '(' & ')' from each line 3. Arrange the lines in matrix expression form by concatination like [1 2 3; 4 5 6; 7 8 9;....] 4. Evaluate the string
Refer the below code...
f=fopen('SampleData.txt','r'); % open file
% collect the text in cell array seperated by \n
textcellarray=textscan(f,'%s','delimiter','\n');
MatrixStr='';
for i=1:length(textcellarray{1})
if(~isempty(textcellarray{1}{i}))
textline=strrep(textcellarray{1}{i},'(',''); % remove '('
textline=strrep(textline,')',''); % remove ')'
% Concatinate the lines as matrix expression
MatrixStr=strcat(MatrixStr,textline,';');
end
end
% Evaluate the matrix expression
FinalMatrix=eval(strcat('[',MatrixStr,']'));
Voir également
Catégories
En savoir plus sur Text Files dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!