How to copy each column to other file faster?

4 views (last 30 days)
Dear matlab expert, I have 2 excel data . The first one is the main data, the other one is the output file. I want to copy each column of the main data to the third column of the output file one by one with the header still on and save it as txt format for each copy. How can copy each column and make a multiple output file all at once?
  2 Comments
Jan
Jan on 28 Nov 2022
Edited: Jan on 28 Nov 2022
Why there are 4 Excel files attached?
What have you tried so far? Which names do you want for the output files? Are you able to import the Excel files already? Then please post the existing code.
What does "faster" mean - faster than what?
adhi dermawan
adhi dermawan on 28 Nov 2022
Sorry sir, I already deleted those 2 files. I am able import the excel
clc;clear; close all;
format long g
filename1 = 'C:\ZTD\MainData.xlsx';
filename2 = 'C:\ZTD\Output.xlsx';
zw = readtable (filename2);
pwv = readmatrix(filename1);
I want to copy and paste the value in each column of the main data to the third column of the Output and save it as 01.txt,02.txt and so on with the same format as the output excel file. There are 1820 column in the Main Data so the result will be 1820 txt file.

Sign in to comment.

Accepted Answer

Arif Hoq
Arif Hoq on 29 Nov 2022
mainfile=readtable('MainData.xlsx');
outputfile=readtable('Output.xlsx');
C=cell(size(mainfile,2),1);
for i=1:size(mainfile,2)
outputfile(:,3)=mainfile(:,i);
result=[outputfile(:,[1 2]) outputfile(:,3) outputfile(:,4)];
C{i,1}=table2cell(result);
end
mat=[C{:}];
% making text files
for j=1:4:size(mat,2)
col= mat(:,j:j+3);
writecell(col,[num2str(j) '.txt'])
end
  3 Comments
Arif Hoq
Arif Hoq on 29 Nov 2022
try this:
mainfile=readtable('MainData.xlsx');
outputfile=readtable('Output.xlsx');
C=cell(size(mainfile,2),1);
for i=1:size(mainfile,2)
outputfile(:,3)=mainfile(:,i);
result=[outputfile(:,[1 2]) outputfile(:,3) outputfile(:,4)];
C{i,1}=table2cell(result);
end
mat=[C{:}];
varname={'Lat','Lon','PWV','STA'};
repitvar=repmat(varname,1,size(mat,2)/4);
mat2=[repitvar;mat];
% making text files
for j=1:4:size(mat2,2)
col= mat2(:,j:j+3);
writecell(col,[num2str(j) '.txt'])
end

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!