Split one colum excel data to 3 colums

조회 수: 8 (최근 30일)
Cecilia Barroso Medina
Cecilia Barroso Medina 2022년 6월 20일
편집: Karim 2022년 6월 20일
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
Cecilia Barroso Medina
Cecilia Barroso Medina 2022년 6월 20일
Yes, sure.
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

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by