필터 지우기
필터 지우기

How can I write a 2D array into a CSV file

조회 수: 14 (최근 30일)
Behrad Ze
Behrad Ze 2022년 2월 17일
답변: Walter Roberson 2022년 2월 17일
I have read an array from a csv file and done some calculations on that. After that, I want to save the array excactly like the CSV file I read.I would be thankful if anyone could help me with this .I have also attached a sample file. thank You so much
sample=readmatrix('71.csv');
Blank=readmatrix('Blank.csv');
x=sample(1,2:end);
y=sample(2:end,1);
RamanArea=5275.6715;
data=sample(2:end,2:end);
dataBlank=Blank(2:end,2:end);
[numRows,numCols] = size(data);
data=data-dataBlank;
for k = 1:numRows
for l = 1:numCols
data(k,l)=data (k,l)/RamanArea;
end
end
for i = 1:numRows
for j = 1:numCols
if (data(i,j)<=0)
data(i,j)=NaN;
end
end
end
xCSV=numRows+1;
yCSV=numCols+1;
xsvArray=csvArray(xCSV,yCSV);
csvArray(2:end,1)=x;
csvArray(1,2:end)=y;
csvwrite('file.csv',csvArray);
csvwrite('file.csv',x,1,0);
figure();
surf(x,y,data);
colorbar();
  댓글 수: 3
Behrad Ze
Behrad Ze 2022년 2월 17일
Exactly. I want to save the missed values as missing or NaN.the A1 is an empty space.I also have several missing values in the array which should be transfered to csv file. The X starts from B1.
Behrad Ze
Behrad Ze 2022년 2월 17일
And the Y starts from A2. Thank You

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

채택된 답변

Walter Roberson
Walter Roberson 2022년 2월 17일
as_cell = [num2cell([nan, x]); num2cell(csvArray)];
as_cell(cellfun(@isnan, as_cell)) = {''};
writecell(as_cell, 'file.csv')

추가 답변 (0개)

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by