Split one colum excel data to 3 colums

7 次查看(过去 30 天)
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 个评论
Chris Burschyk
Chris Burschyk 2022-6-20
In the Import Tool you can use "Fixed Width" instead of "Delimited". Then just add and drag the separators to i.e. the commata.

请先登录,再进行评论。

回答(1 个)

Karim
Karim 2022-6-20
编辑:Karim 2022-6-20
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

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by