how convert txt file into excel file ?

3 views (last 30 days)
kH
kH on 18 Nov 2019
Answered: Raunak Gupta on 21 Nov 2019
Eg :
text file contains
kar_po_data_3_table.kar_po_data_3_basic.q2p_rc_m_ta_h3_cm_vc[0]=0.079387;
kar_po_data_3_table.kar_po_data_3_basic.p2e_rcs_m_ta_h3_cm_vc[1]=0.04785;
....
....
.....
final output : Excel file
kar_po_data_3_table kar_po_data_3_basic.q2p_rc_m_ta_h3_cm_vc[0] 0.03874
kar_po_data_3_basic.p2e_rcs_m_ta_h3_cm_vc[1] 0.04785
.... ...
.... ....
.... ....
.... .....

Answers (1)

Raunak Gupta
Raunak Gupta on 21 Nov 2019
Hi,
Following Code might help write csv file in above format
text_file = fileread('test_text.txt');
content = regexp(text_file, ';', 'split');
content( cellfun(@isempty,content) ) = [];
table_to_write = cell2table(cell(size(content,2),3));
for i=1:size(content,2)
string_cell = content(1,i);
actual_string = string_cell{1};
first = regexp(actual_string, '=', 'split');
first( cellfun(@isempty,first) ) = [];
third_arg = char(first(1,2));
second = regexp(first{1}, '.', 'split');
second( cellfun(@isempty,second) ) = [];
first_arg = strtrim(second{1});
second_arg = strjoin(second(1,2:3),'\.');
table_to_write.Var1(i) = {first_arg};
table_to_write.Var2(i) = {second_arg};
table_to_write.Var3(i) = {third_arg};
end
writetable(table_to_write,'csvFile.csv','WriteVariableNames',false,'Delimiter',',');
For Reading the csv file again in MATLAB note that the delimiter must be 'comma'(',').

Community Treasure Hunt

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

Start Hunting!