Probelm with 'writecell'

조회 수: 1 (최근 30일)
junghyun pyo
junghyun pyo 2019년 7월 10일
댓글: Guillaume 2019년 7월 15일
I just want to delet first line form my data.
It's .csv file, and raw data has two line for text and others are number.
This is what i trying to do.
m = readcell('rawdata.csv');
m(1,:) = [];
writecell(m, 'modified_data.csv');
but it doesn't work.
thanks for your help.

답변 (2개)

Star Strider
Star Strider 2019년 7월 10일
Try this:
m = readcell('rawdata.csv');
writecell(m(2:end,:), 'modified_data.csv');
If that does not work, upload (attach) ‘rawdata.csv’ so we can experiment with it.
  댓글 수: 1
junghyun pyo
junghyun pyo 2019년 7월 10일
편집: junghyun pyo 2019년 7월 10일
Thanks!
I just try to do it with your code, but there is some problem.
Because my data has 0 (i guess it is emthy) then using 'writecell' made an error about 'missing type is not supported'
I may handle this error like this
m = readcell('20190529_20_1.csv');
n = table2cell(m(2:end,:));
writetable(n, '20190529_20_1.csv')

댓글을 달려면 로그인하십시오.


Guillaume
Guillaume 2019년 7월 10일
If the only thing you're trying to do is remove the first line of a text file, then parsing the file (with csvread, readcell, or any similar function) is completely overkill and just slows you down.
filecontent = fileread('rawdata.csv');
without1stline = regexp(filecontent, '(?<=[\n\r]+)[^\n\r].*', 'match', 'once')
fid = fopen('modifed_data.csv', 'w');
fwrite(fid, without1stline);
fclose(fid);
  댓글 수: 2
junghyun pyo
junghyun pyo 2019년 7월 15일
It works great!
Then i'd like to ask you to how can i use codes that you answer in this topic and the other one(for uigetfile).
I just try to, but it just return filename in file.
Thanks for your help.
[filename, path] = uigetfile('*.csv');
filecontent = fileread(filename);
[~, base, extension] = fileparts(filename);
without1stline = regexp(filecontent, '(?<=[\n\r]+)[^\n\r].*', 'match', 'once');
newfilename = [base, '_modified', extension];
fid = fopen(newfilename, 'w');
fwrite(fid, fullfile(path, newfilename));
fclose(fid);
Guillaume
Guillaume 2019년 7월 15일
[filename, path] = uigetfile('*.csv'); %ask user for a csv file
assert(~isequal(filename, 0), 'No file selected. Aborting');
filecontent = fileread(fullfile(path, filename)); %read file, using full path
without1stline = regexp(filecontent, '(?<=[\n\r]+)[^\n\r].*', 'match', 'once'); %remove 1st line of file
[~, base, extension] = fileparts(filename); %split input file name into base and extension
newfilename = [base, '_modified', extension]; %append '_modified' to base filename and add extension back
fid = fopen(fullfile(path, newfilename), 'w'); %create new file
fwrite(fid, without1stline);
fclose(fid);

댓글을 달려면 로그인하십시오.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by