Split one colum excel data to 3 colums

2 visualizaciones (últimos 30 días)
Cecilia Barroso Medina
Cecilia Barroso Medina el 20 de Jun. de 2022
Editada: Karim el 20 de Jun. de 2022
Hi,
I'm just starting with Matlab and I'm still struggling to do even the simplest things.
I have an excel document with a column that collects 3 data separated by commas (see picture)
My idea is to separate those three data in three different columns to be able to use them (so I would have in separate columns s, mm and N). I have tried several ideas that I have seen in the forums, but I only manage to separate all the data and leave them as a single column.
I tried readcell and reshape.
Could you help me?
  3 comentarios
Cecilia Barroso Medina
Cecilia Barroso Medina el 20 de Jun. de 2022
Yes, sure.
Chris Burschyk
Chris Burschyk el 20 de Jun. de 2022
In the Import Tool you can use "Fixed Width" instead of "Delimited". Then just add and drag the separators to i.e. the commata.

Iniciar sesión para comentar.

Respuestas (1)

Karim
Karim el 20 de Jun. de 2022
Editada: Karim el 20 de Jun. de 2022
Hey,
Due to both the ' " ' and the comma's I would to read the file as strings and then extract the data you need. See the code below. Hope it helps.
fileID = fopen('RawData_1.csv');
% read the whole file, interpret each line as a string
MyText = textscan(fileID, '%s%[^\n\r]', 'Delimiter', '', 'WhiteSpace', '', 'ReturnOnError', false);
% convert the cell array into a string array, for easier indexing
MyText = string(strtrim(MyText{1}));
% first the start locations
StartLineIdx = 5;
% extract the usefull strings from the data
MyData = MyText(StartLineIdx:end);
% delete the " characters
MyData = strrep(MyData,'"','');
% use the comma to split each string
MyData = split(MyData,',');
% convert the strings into a numeric array
MyData = str2double(MyData);
% extract the columns of intrest
Data_s = MyData(:,1);
Data_mm = MyData(:,2);
Data_N = MyData(:,3);
figure
subplot(2,1,1)
plot(Data_s,Data_mm)
ylabel('data mm')
grid on
subplot(2,1,2)
plot(Data_s,Data_N)
ylabel('data N')
xlabel('data s')
grid on

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by