Why is my table being saved in a wrong way in a CSV file?

2 visualizaciones (últimos 30 días)
Raffaele
Raffaele el 10 de Feb. de 2024
Comentada: Stephen23 el 23 de Feb. de 2024
I'm writing a program that creates a table and saves it in a .csv file using the writeTable command. When I print the table in the command window, it displays as I intended it to be, but when I open the .csv file, the table appears "misplaced", as if the program fails to recognize the field and data.
Here's the code for the program:
nct = 0; % NCT - Numero Catture Totali del DataSet. dI DEFAULT A 0
varTypes = {'string', 'string', 'string', 'double', 'double', 'string', 'double', 'double', 'string', 'double', 'double', 'string', 'double', 'double'};
varNames = {'User', 'Device', 'Move1', 'Start1', 'End1', 'Move2', 'Start2', 'End2', 'Move3', 'Start3', 'End3', 'Move4', 'Start4', 'End4'};
for n = 1:5 %Per ogni utente nel dataset
for k = 1:4 %Per ogni cattura dell'utente
nct=nct+1;
end
end
sz = [nct 14];
movementDataset = table('size', sz, 'variableTypes', varTypes, 'VariableNames', varNames);
nct = 0; %Riporto nct a 0 per utilizzarlo nel ciclo for che segue;
for n = 1:5 %Per ogni utente nel dataset
for k = 1:4 %Per ogni cattura dell'utente
nct=nct+1;
movementDataset{nct, 1} = n; %Nome utente
movementDataset{nct, 2} = "Iphone 15"; %Device
movementDataset{nct, 3} = "AAA AAA"; %Primo movimento
movementDataset{nct, 4} = 0; %Inizio Primo movimento
movementDataset{nct, 5} = 1; %Fine Primo movimento
movementDataset{nct, 6} = "EEE E E E"; %Secondo movimento
movementDataset{nct, 7} = 3; %Inizio Secondo movimento
movementDataset{nct, 8} = 4; %Fine Secondo movimento
movementDataset{nct, 9} = "c"; %Terzo movimento
movementDataset{nct, 10} = 6; %Inizio Secondo movimento
movementDataset{nct, 11} = 7; %Fine Terzo movimento
movementDataset{nct, 12} = "D"; %Quarto movimento
movementDataset{nct, 13} = 9; %Inizio Quarto movimento
movementDataset{nct, 14} = 10; %Fine Quarto movimento
end
end
disp(movementDataset);
writetable(movementDataset, "movementDataset.csv");
And here's the .csv file
  2 comentarios
Siddharth Bhutiya
Siddharth Bhutiya el 22 de Feb. de 2024
I just tried this on MATLAB online and it seems to load the csv file correctly. Could you attach the file. Also what platform (Windows, Mac, Linux) are you on?
Stephen23
Stephen23 el 23 de Feb. de 2024
"Also what platform (Windows, Mac, Linux) are you on?"
The most important question is: what delimiter does your OS' locale specify?

Iniciar sesión para comentar.

Respuestas (1)

Stephen23
Stephen23 el 22 de Feb. de 2024
Editada: Stephen23 el 22 de Feb. de 2024
"Why is my table being saved in a wrong way in a CSV file?"
Why do you think that MATLAB is doing something wrong?
"but when I open the .csv file, the table appears "misplaced", as if the program fails to recognize the field and data."
MATLAB creates the CSV file correctly (it is a perfectly correct comma separated file).
The problem is that you are opening the CSV file (correctly encoded using delimiter commas) on a computer whose locale defines a semi-colon delimiter or some other delimiter character. Most likely because you speak a language that uses decimal commas, not decimal points.
You can either tell MATLAB to save using a delimiter character of your choice (e.g. semi-colon, see the DELIMITER option in the WRITETABLE documentation).
OR
you can tell your CSV-reading application to open it properly, e.g. by telling it that the CSV file uses a comma delimiter. For example, if you are opening the CSV file in MS Excel, create a new line at the very top of the file before all of the data and paste this text (and nothing else on that line):
sep=,
and then try opening the file again in Excel.
OR
you could change your locale settings to use a comma delimiter and decimal point.

Categorías

Más información sobre Text Data Preparation en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by