Import data does not properly
Mostrar comentarios más antiguos
Dear all,
I moved from the R2024 to R2026 version and now when I import data it shows a very curious data representations for e.g. two column
197 0000 0000->0
196 8000 8000->1
196 6000 6000->0
196 4000 4000->0
rather than
197,0000 0,494778
196,8000 1,07849
196,6000 0,965887
196,4000 0,839238
I did 'Set type' to Number, but no real effect.
How can I manage it?
Thanks
Best
Marco
9 comentarios
Marco Sette
el 12 de Mayo de 2026 a las 14:44
Movida: Steven Lord
el 12 de Mayo de 2026 a las 15:50
Steven Lord
el 12 de Mayo de 2026 a las 15:55
Please attach a small segment of a file formatted the same way the file that didn't import as you expected is formatted. Also please describe in more detail how exactly you imported the file (posting the commands or showing pictures of the import dialog if you imported it that way.)
Without those details, it's likely going to be extremely difficult or impossible to offer any concrete advice.
dpb
el 12 de Mayo de 2026 a las 16:53
It looks as though the file is formatted for the decimal point character to be a comma, not the default US period. Set the "Decimal Separator" in the import tool settings.
The more robust way is to use readmatrix that allows for control with the named parameter or by using an import options struct.
Andreas Goser
hace alrededor de 12 horas
The user contacted Technical Support and asked the engineer to also close the loop with the community here what helped.
Marco Sette
hace alrededor de 12 horas
dpb
hace alrededor de 11 horas
@Andreas Goser indicated earlier you had so would seem they should provide a solution, yes.
But, it might be useful for the Community if you were to follow up with @Steven Lord's suggestion to attach a representative file that fails and the code/method used to try to read it.
If the file doesn't use the comma for the decimal separator, something else is happening but only seeing the file structure itself would help.
Have you tried
data=readmatrix('yourfullyqualifiedfilename');
to see if it can read it successfully?
Image Analyst
hace alrededor de 11 horas
Yes, if you have paid for technical support, calling them and talking to a live person will almost always be the fastest way to a solution. And since you're not attaching your data or code (despite being asked), we'll just be throwing out guesses here. And I cannot even begin to guess how you think 0000->0 should be turned into "0,494778". Same for the other strings in the second column - no idea where those desired numbers come from.
Marco Sette
hace alrededor de 11 horas
Editada: Walter Roberson
hace alrededor de 4 horas
Andreas Goser
hace alrededor de 11 horas
I have not considered this leads to this chain of comments :-D Let me try to clarify:
- It is totally fine users leverage multiple channels
- I am always a fan however of closing the loop with the community so it is known what the outcome of a support request is. At times it needs to be short as we cannot disclose details of a customer application. It is also a bit of coincidence a MathWorks staffer notices MATLAB Answers questions also being asked to support teams.
For this issue:
- The theories and hints in this thread are actually very promising! That was partly a reason I informed the support engineer about this post
- The user provided more technical information with support, so I suppose we will resolve it and the community does not have to help. But I surley do not want to discourage good intention.
Respuestas (3)
Marco Sette
hace alrededor de 11 horas
0 votos
Marco Sette
hace alrededor de 10 horas
0 votos
There's a paperclip icon with which you can attach a file. I pasted into a local file and uploaded a copy here...
The "simple" text file is far more than just numeric data, however. And, as I suspected, it IS written with the decimal separator being the comma, not a period; that's why the line
260,0000 0,626894 246,062
was interpreted as
260, 0, 0, 626894, 246, 062
because there are three sets of two numbers in the line as it will be interpreted when a comma is a delimiter, not a decimal point.
L=readlines('marco.txt'); % read full file as string array
iXY=find(startsWith(L,'XYDATA')); % find the XY Data section start
data=readmatrix('marco.txt','NumHeaderLines',iXY,'DecimalSeparator',',')
clear l iXY % get rid of uneeded variables
An alternate way w/o the extra copy in memory as text would be
fid=fopen('marco.txt','r'); % open, get file handle
iXY=0; % initialize line counter
while ~feof(fid) % loop until run our of data
iXY=iXY+1; % increment counter
l=fgetl(fid); % read a line
if startsWith(l,'XYDATA'); break; end % found the XY Data section start, quit
end
fid=fclose(fid); % close the file
data=readmatrix('marco.txt','NumHeaderLines',iXY,'DecimalSeparator',',')
clear l fid iXY % get rid of uneeded variables
2 comentarios
Marco Sette
hace alrededor de 8 horas
Movida: dpb
hace alrededor de 4 horas
@Marco, thanks for the response; as for not being expert, we all had to start from somewhere first...for the Q? about three variables instead of an array, there aren't as many tools in the bag; the venerable one textread has been relegated to the "not recommended" category in favor of textscan although it, other than low-level i/o via fscanf is the only way to return individually named variables since textscan returns a cell array rather than multiple variables. The issues there are that neither of those options has been extended to handle the 'DecimalSeparator' parameter so one has to convert the comma first.
One can, of course, simply assign names to the columns although that is laborious. Creating a struct of the array with field names to match the desired is another generic coding practice. Or, use a table that can have variables with their names.
folder=''; % use your file location
fqn=fullfile(folder,'marco.txt'); % build fully qualified name
fid=fopen(fqn,'r'); % open, get file handle
iXY=0; % initialize line counter
while ~feof(fid) % loop until run our of data
iXY=iXY+1; % increment counter
l=fgetl(fid); % read a line
if startsWith(l,'XYDATA'); break; end % found the XY Data section start, quit
end
fid=fclose(fid); % close the file
data=readtable(fqn,'NumHeaderLines',iXY,'DecimalSeparator',',');
data.Properties.VariableNames={'X','Y','Z'}
clear l fid iXY % get rid of uneeded variables
also illustrating good practice on builing file names. Another very useful paradigm in that regards is to use
d=dir('*.txt'); % some appropriate wild card expression here
for i=1:numel(d)
fqn=fullfile(d(i).folder,d(i).name);
...
end
Categorías
Más información sobre Data Import and Export en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!