Read data from .txt file
113 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Turbulence Analysis
el 11 de Oct. de 2022
Comentada: dpb
el 11 de Oct. de 2022
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 comentarios
Respuesta aceptada
David Hill
el 11 de Oct. de 2022
Editada: David Hill
el 11 de Oct. de 2022
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)
Más respuestas (2)
dpb
el 11 de Oct. de 2022
Editada: dpb
el 11 de Oct. de 2022
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 comentarios
dpb
el 11 de Oct. de 2022
Editada: dpb
el 11 de Oct. de 2022
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
el 11 de Oct. de 2022
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 comentario
dpb
el 11 de Oct. de 2022
@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?
Ver también
Categorías
Más información sobre Workspace Variables and MAT-Files en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!