Split one colum excel data to 3 colums

2 views (last 30 days)
Cecilia Barroso Medina
Cecilia Barroso Medina on 20 Jun 2022
Edited: Karim on 20 Jun 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 Comments
Chris Burschyk
Chris Burschyk on 20 Jun 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.

Sign in to comment.

Answers (1)

Karim
Karim on 20 Jun 2022
Edited: Karim on 20 Jun 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

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!