how convert txt file into excel file ?

1 回表示 (過去 30 日間)
kH
kH 2019 年 11 月 18 日
回答済み: Raunak Gupta 2019 年 11 月 21 日
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
.... ...
.... ....
.... ....
.... .....

回答 (1 件)

Raunak Gupta
Raunak Gupta 2019 年 11 月 21 日
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'(',').

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by