フィルターのクリア

Export Excel values and characters to a txt file

2 ビュー (過去 30 日間)
Thibaut
Thibaut 2020 年 4 月 16 日
コメント済み: Thibaut 2020 年 4 月 17 日
Good afternoon,
Please I am trying to export data from Excel in order to write them in a txt file. The Excel file contains values and characters, I tried to use fprintf and dlmwrite but I always have a trouble with the format. Furthermore, I don't know how to use fprintf without replacing what was already present in the txt file.
The result I would like to have is contained in the txt Jeu2.txt (wrote it manually). I copied the colums A1:H10001 then pasted, copied J1:Q10001 then pasted. I need to do this for the 3 sheets.
This is what I have tried so far :
%Creation fichiers .mac
i=1;
Nom_jeux='Bon_Format_Jeu_%d.xlsx';
str = sprintf(Nom_jeux,i);
Bon_Format='Jeu_%d.mac';
New_FORMAT = sprintf(Bon_Format,i);
fid = fopen('New_FORMAT','wt');
fprintf(fid, '!ACCELEROGRAMME HORIZONTALE SELON X POUR LE JEU 1 ');
Bon_Format='Bon_Format_Jeu_%d.xlsx';
str = sprintf(Bon_Format,i);
TAB='A%d:H%d';
A1=1;
A2=10001;
str2=sprintf(TAB,A1,A2);
[num,Temps,Raw]=xlsread(str,'Feuil1',str2);
fprintf(fid,Raw,);
I would be really grateful if someone could help me !
Thanks,
Thibaut

採用された回答

Image Analyst
Image Analyst 2020 年 4 月 16 日
編集済み: Image Analyst 2020 年 4 月 16 日
This seems to work:
% Creation fichiers .mac
folder = pwd; % Wherever....
i=1;
Nom_jeux = sprintf('Bon_Format_Jeu_%d.xlsx', i);
xlFullFileName = fullfile(folder, Nom_jeux);
if isfile(xlFullFileName)
% If Excel workbook exists...
fprintf('Processing %s...\n', xlFullFileName);
% Read in Excel workbook from the correct range.
A1=1;
A2=10001;
cellRange = sprintf('A%d:H%d',A1,A2);
[numbers, strings, Raw] = xlsread(xlFullFileName, 'Feuil1', cellRange);
[rows, columns] = size(Raw);
% Create the filename of our new text file that we will write.
textBaseFileName = sprintf('Jeu_%d.txt', i);
textFullFileName = fullfile(folder, textBaseFileName);
% Open text file.
fid = fopen(textFullFileName, 'wt');
fprintf(fid, '!ACCELEROGRAMME HORIZONTALE SELON X POUR LE JEU 1 ');
fprintf(fid, '!================================================================================================================\n');
fprintf(fid, '!=========================== ============================\n');
fprintf(fid, '!=========================== ACCELEROGRAMME HORIZONTALE SELON X POUR LE JEU 1 ============================\n');
fprintf(fid, '!=========================== ============================\n');
fprintf(fid, '!================================================================================================================\n');
fprintf(fid, '\n');
fprintf(fid, '*DIM,acc_X,table,10001,1,1,TIME,\n');
fprintf(fid, '\n');
fprintf(fid, '!=============================================================\n');
fprintf(fid, '!=========== Temps ================\n');
fprintf(fid, '!=============================================================\n');
% Dump data into text file row by row.
for row = 1 : rows
% Make string to write out. Add spaces, or adjust field widths as desired.
thisLine = sprintf('%s %5d , %5d , %5s = %0.9f', ...
Raw{row, 1}, Raw{row, 2}, Raw{row, 4}, Raw{row, 6}, Raw{row, 8});
fprintf('%s\n', thisLine); % To command window.
fprintf(fid, '%s\n', thisLine); % To file.
end
% Close the file.
fclose(fid);
if contains(computer, 'PCWIN')
% Open the text file if using Windows.
winopen(textFullFileName);
end
else
message = sprintf('File not found:\n%s', xlFullFileName);
uiwait(errordlg(message));
end
  1 件のコメント
Thibaut
Thibaut 2020 年 4 月 17 日
Thank you really much !

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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