i have to get similar output as i have uploaded.why my data is getting repeated again and again?
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (2)
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
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.
dpb
on 10 Oct 2022
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
3 Comments
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?
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!