i have to get similar output as i have uploaded.why my data is getting repeated again and again?

조회 수: 1 (최근 30일)
T=readtable('input.csv');
th=[0:1:3]';
for i= 1:size(th)
reph=T{i,end-1};
imth=T{i,end};
result(i)=reph+1i*imth;
end
result_data=result';
for j=1:nume1(th)
result_data2(:,j)=result_data;
end
data1=[th result_data2];
csvwrite('sample.csv',data1);
my same data is getting repeated again and again
I have read input.csv file
and output should be saved in .csv format and it should exactly look similar to file plot.csv ..
2021a version

답변 (2개)

David Hill
David Hill 2022년 10월 10일
T=readtable('input.csv');
th=[0:1:3]';
result=[th,reshape(T.re+T.im*1i,size(th,1),[])];
csvwrite('sample.csv',result);
  댓글 수: 3
riki singh
riki singh 2022년 10월 10일
i have all data in form of which is shown in input.csv file
i have to write a code in such a way that my final output looks like plot.csv file.exact replica of that file
dpb
dpb 2022년 10월 10일
Above does so with the one small task left for you to add the first row into the result cell array before calling writecell.
Only thing I'd suggest to change to generalize would be to replace the hardcoded line
th=[0:1:3]';
with
th=unique(T.Th);
to make the code more general if/when the number of elements in Th changes.

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


dpb
dpb 2022년 10월 10일
Carrying on from @David Hill's most excellent start,
tT=readtable(websave('input.csv','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1151360/input.csv'));
u=unique(tT.Th); % in case number changes in future, be general
tT.C=complex(tT.re,tT.im); % convenience of short v name
out=[{'Th'} compose('p=%d',u.')]; % the header row for starters
out=[out;num2cell(u) num2cell(reshape(tT.C,numel(u),[]))]; % finish off the output cell array
writecell(out,'plot.csv') % and save
type plot.csv
Th,p=0,p=1,p=2,p=3 0,3.2+8i,5.2+9i,9.2-40i,6.2+7.8i 1,5.2+8.5i,6.2+7i,9.2-9.5i,5.2-6.3i 2,4.2+3.2i,7.2+5i,8.2-9.8i,6.23-4.5i 3,6.2+6.3i,8.2+2i,7.2+9.8i,4.2+4.5i
  댓글 수: 3
dpb
dpb 2022년 10월 11일
편집: dpb 2022년 10월 11일
You want something that works or a klunk?
You can recast it however you want but there's no point in having MATLAB and not use it as it's designed to be used. The name is, after all, "MATrix LABoratory" for a reason.
dpb
dpb 2022년 10월 11일
So, if you work thru each line at command line and see what does and refer back to the doc for any function with which you're not familiar, what, specifically do you not understand/don't follow after seeing its result?

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by