Read data from .txt file

276 ビュー (過去 30 日間)
Turbulence Analysis
Turbulence Analysis 2022 年 10 月 11 日
コメント済み: dpb 2022 年 10 月 11 日
Hi,
I have a .txt file which got 1442 rows and 100 columns. I nned to read and store this data in a sepertae array, lets say A.
I tried A = importdata('mydata.txt');
Unfortunately here the all the datas in the different columns of .txt file got stored in the same column.
Any solution for this ??
Thanks !!
  4 件のコメント
Turbulence Analysis
Turbulence Analysis 2022 年 10 月 11 日
Thanks, please find the attached file..
with readmatrix, I got a below error
Undefined function or variable 'readmatrix'.
Turbulence Analysis
Turbulence Analysis 2022 年 10 月 11 日
Ghazwan,
Although readtable works, however it reads wrongly. for instance 1,2345 reads as 2345

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

採用された回答

David Hill
David Hill 2022 年 10 月 11 日
編集済み: David Hill 2022 年 10 月 11 日
Below works. If your version of matlab does not have readmatrix you could just copy the m.mat file attached.
m=readmatrix('mydata.txt','decimalSeparator',',');
m(1:5,1:10)
ans = 5×10
-360.0000 1.2028 1.2323 1.2180 1.1786 1.1867 1.1411 1.1848 1.1591 1.1430 -359.5000 1.2028 1.2323 1.2138 1.1786 1.1696 1.1368 1.1805 1.1549 1.1387 -359.0000 1.1943 1.2237 1.2095 1.1743 1.1739 1.1325 1.1762 1.1463 1.1430 -358.5000 1.1943 1.2237 1.2138 1.1701 1.1653 1.1240 1.1720 1.1506 1.1344 -358.0000 1.1943 1.2152 1.2052 1.1658 1.1610 1.1240 1.1677 1.1463 1.1344
  1 件のコメント
Turbulence Analysis
Turbulence Analysis 2022 年 10 月 11 日
Thank you very much, David
But I have many such files to work on. Since my version of the matlab doesn't support the readmatrix, so whats the possible solution ?

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

その他の回答 (2 件)

dpb
dpb 2022 年 10 月 11 日
編集済み: dpb 2022 年 10 月 11 日
The file and your local system LOCALE setting aren't in synch it appears -- the default MATLAB locale is US which uses the decimal point as the "." character, not the comma.
This can be fixed by using an import options object with some fixups to handle...
opt=opt=detectImportOptions('mydata.txt','delimiter','\t', ...
'DecimalSeparator',',', ...
'VariableNamesLine',1, ...
'VariableUnitsLine',2);
tT=readtable('mydata.txt',opt);
and joy should ensue...
readmatrix wasn't introduced until R2019a; somewhat belatedly as readtable came along in R2013b.
  2 件のコメント
Turbulence Analysis
Turbulence Analysis 2022 年 10 月 11 日
Dpb,
I getting a below shown error with your script.
Error using detectImportOptions
'DecimalSeparator' is not a recognized parameter. For a list of valid name-value pair arguments, see the documentation for detectImportOptions.
Error in detectImportOptions>getTypedParser/parsefcn (line 352)
p.parse(args{:});
Error in detectImportOptions>textArgs (line 384)
args = parser(otherArgs);
Error in detectImportOptions (line 265)
args = textArgs(p.Unmatched);
dpb
dpb 2022 年 10 月 11 日
編集済み: dpb 2022 年 10 月 11 日
Which release are you using?
As the others say, upgrade if can at all possibly; I don't remember just when the decimal separator came in; it is in by R2020b that I'm using...
But, if not possible, yours is a very clean file, the global substitution of a "." for the "," will work and isn't difficult. Read the file and spit it back out again...
fid=fopen('mydata.txt','r'); % open the file as stream ("binary") r access
f=fread(fid,'*char'); % suck it up...
f(f==',')='.'; % turn "," into "."
fclose(fid) % done with input
fid=fopen('mydata.csv','w'); % create new file for security
fwrite(fid,f); % write the new stuff out
fid=fclose(fid); % and cleanup after ourselves
clear fid f
Now traditional readtable will work on the new file...
You can also
fid=fopen('mydata.txt','r+'); % open the file as stream ("binary") r/w access
f=fread(fid,'*char'); % suck it up...
f(f==',')='.'; % turn "," into "."
frewind(fid) % go back to beginning
fwrite(fid,f); % write the new stuff over old file
fid=fclose(fid); % and cleanup after ourselves
clear fid f

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


Walter Roberson
Walter Roberson 2022 年 10 月 11 日
We recommend upgrading to get the newer decimal separator support.
If that is not possible then you will need to read the file as text and replace the comma with period that then interpret the numbers. For example you might take the first line and regexp 'split' to get the variable names and you might textscan the characters with HeaderLines 1 and format '' (the empty string) to get the data and cell2mat and array2table with the variable names you pulled out
  1 件のコメント
dpb
dpb 2022 年 10 月 11 日
@Walter Roberson - you're pretty up on when TMW has done stuff and the more esoteric thingies w/ the OS interfaces -- do you know if/when the system LOCALE setting and MATLAB paying attention to it for non-US systems began? Did it follow along at same time as the 'DecimalSeparator' support, I suppose?

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

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by