I have a set of numbers of XY data (.csv format), with each XY value in the format: 2.024890e 002,2.069117e 003. How do I convert to format 20248.90 2069.117 instead, i.e. nonscientific and spaces not commas? I have attached the file :)
Thank you, and sorry for the really basic question!

 Akzeptierte Antwort

Stephan
Stephan am 17 Sep. 2019

0 Stimmen

% Setup the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 2);
% Specify range and delimiter
opts.DataLines = [1, Inf];
opts.Delimiter = ",";
% Specify column names and types
opts.PreserveVariableNames = true;
opts.VariableNames = ["x", "y"];
opts.VariableTypes = ["double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Import the data
xy = readtable("YOUR_FULL_FILE_PATH_HERE\xy.csv", opts);
% Convert to output type
xy = table2array(xy);
% Clear temporary variables
clear opts

5 Kommentare

This is way overkill for such a simple file.
xy = readtable('test.csv'); %if a table is desired
or
xy = readmatrix('test.csv'); %if an array is desired and on R2019a or later
or
xy = csvread('test.csv'); %any matlab version
Important: The way matlab displays the content of xy at the command prompt or in the variable browser has nothing to do with the way the data is encoded in the file. It doesn't matter if the numbers are encoded in scientific notation or not in the file, the way it is displayed in matlab at the command prompt is only controlled by format.
format longg
%or
format shortg
will avoid displaying in scientific notation as much as possible.
Michel Nieuwoudt
Michel Nieuwoudt am 17 Sep. 2019
Hi Stephan and Guillame,
Thank you for the codes. I tried to run Stephan's code but it seems I don't have the function delimitedTextImportOptions, as I got an error saying
Undefined function or variable 'delimitedTextImportOptions'.
I also ran Guillame's code, and could open the file in the format I need, but when I saved the file it saved it in sceintiric format (I use save xy xy -ascii). Is there another way to save it in non-scientific format perhaps?
Thank you again for your help!
Use csvwrite or dlmwrite. csvwrite uses 5 digits of precision only. dlmwrite lets you specify it.
csvwrite('xy.txt', xy); %5 digits only
%or
dlmwrite('xy.txt', xy, 'precision', '7'); %7 digits
%or
dlmwrite('xy.txt', xy, 'precision', '%3.7f'); %to specify the format directly. See fprintf doc for format specification
Stephan
Stephan am 17 Sep. 2019
Bearbeitet: Stephan am 17 Sep. 2019
You should follow Guillaumes advise - the code i used is the lazy way: Matlab auto generated code from the import tool (this is why it is that large). Therefore it is useful if you provide your Matlab release when asking questions here. If you do not, i have to assume you use the latest release...
Michel Nieuwoudt
Michel Nieuwoudt am 17 Sep. 2019
Thank you so much Guillaume :) , and thank you Stephan. I have the 2018b version; will add this next time.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by