フィルターのクリア

Write a simple txt files made by strings and numbers

1 回表示 (過去 30 日間)
Guglielmo Giambartolomei
Guglielmo Giambartolomei 2019 年 10 月 9 日
Good evening,
I would like to write a simple .txt file with data I already have in my workspace. In particular I would like to create a 9x2 matrix with strings and numerical values. The code I wrote is the following. I think there are some problems with the %.... Thank you for your help.
tests = vertcat('CT4H12X9','CT5H12X9','CT5H12X8','CT6H12X7','CT7H12X2','CT8H12X2','CT7H12X5','CT8H12X6');
y_acc_max=[CT4H12X9_max_acc,CT5H12X9_max_acc,CT5H12X8_max_acc,CT6H12X7_max_acc,CT7H12X2_max_acc,CT8H12X2_max_acc,CT7H12X5_max_acc,CT8H12X6_max_acc];
A = [tests; y_acc_max];
fileID = fopen('acc_max.txt','w');
fprintf(fileID,'%6s %12s\n','Test','Maximum acceleration');
fprintf(fileID,'%6.2s %12.8f\r\n',A);
fclose(fileID);
  4 件のコメント
J. Alex Lee
J. Alex Lee 2019 年 10 月 10 日
Where is your issue/error? Assuming y_acc_max is a 1x8 double, I would think you have an error trying to concatenate your char array and double array
A = [tests; y_acc_max];
Also, what happens to your code when your labels (tests) have different number of characters?
Adam Danz
Adam Danz 2019 年 10 月 10 日
" In particular I would like to create a 9x2 matrix with strings and numerical values. "
"A is the matrix 9x2 that I would like to create. The first row is the title ..."
What you are describing is not a "matrix". Matrices are only numeric. You're probably working with a cell array or a table.
This is why I asked if you could provide us with "A" or at least a few rows of A so we can see what you're doing.

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

採用された回答

J. Alex Lee
J. Alex Lee 2019 年 10 月 10 日
With minimal changes to what you have (change space to tab too)
tests = vertcat('CT4H12X9','CT5H12X9','CT5H12X8','CT6H12X7','CT7H12X2','CT8H12X2','CT7H12X5','CT8H12X6');
% y_acc_max=[CT4H12X9_max_acc,CT5H12X9_max_acc,CT5H12X8_max_acc,CT6H12X7_max_acc,CT7H12X2_max_acc,CT8H12X2_max_acc,CT7H12X5_max_acc,CT8H12X6_max_acc];
y_acc_max = rand(1,8);
% A = [tests; y_acc_max];
fileID = fopen('acc_max.txt','w');
fprintf(fileID,'%8s\t%12s\r\n','Test','Maximum acceleration');
for irow = 1:length(y_acc_max)
fprintf(fileID,'%8s\t%12.8f\r\n',tests(irow,:),y_acc_max(irow));
end
fclose(fileID);
Another option with minimal change
tests = {'CT4H12X9','CT5H12X9','CT5H12X8','CT6H12X7','CT7H12X2','CT8H12X2','CT7H12X5','CT8H12X6'};
% y_acc_max=[CT4H12X9_max_acc,CT5H12X9_max_acc,CT5H12X8_max_acc,CT6H12X7_max_acc,CT7H12X2_max_acc,CT8H12X2_max_acc,CT7H12X5_max_acc,CT8H12X6_max_acc];
y_acc_max = rand(1,8);
A = [tests; num2cell(y_acc_max)]
fileID = fopen('acc_max_2.txt','w');
fprintf(fileID,'%8s\t%12s\r\n','Test','Maximum acceleration');
fprintf(fileID,'%8s\t%12.8f\r\n',A{:});
fclose(fileID);
Maybe also look into "table" variable type. With the second example
t = table(y_acc_max','VariableNames',{'Maximum acceleration'},'RowNames',tests)
t.Properties.DimensionNames{1} = 'Test'
writetable(t,'acc_max_t.txt','WriteRowNames',true,'Delimiter','\t')
  1 件のコメント
Guglielmo Giambartolomei
Guglielmo Giambartolomei 2019 年 10 月 10 日
J. Alex Lee thank you so much! I used the second option and it worked fine! I didn't know it was so hard to write a simple txt file with Matlab.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by