Clear Filters
Clear Filters

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

1 view (last 30 days)
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

Answers (2)

David Hill
David Hill on 10 Oct 2022
T=readtable('input.csv');
th=[0:1:3]';
result=[th,reshape(T.re+T.im*1i,size(th,1),[])];
csvwrite('sample.csv',result);
  3 Comments
riki singh
riki singh on 10 Oct 2022
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 on 10 Oct 2022
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.

Sign in to comment.


dpb
dpb on 10 Oct 2022
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 Comments
dpb
dpb on 11 Oct 2022
Edited: dpb on 11 Oct 2022
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 on 11 Oct 2022
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?

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!