Write a simple txt files made by strings and numbers
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Guglielmo Giambartolomei
le 9 Oct 2019
Commenté : Guglielmo Giambartolomei
le 10 Oct 2019
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 commentaires
J. Alex Lee
le 10 Oct 2019
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
le 10 Oct 2019
" 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.
Réponse acceptée
J. Alex Lee
le 10 Oct 2019
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')
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!