Write a simple txt files made by strings and numbers

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 comentarios

Adam Danz
Adam Danz el 9 de Oct. de 2019
What is in 'A' and what are the problems? Could you provide us with 'A'?
A is the matrix 9x2 that I would like to create. The first row is the title ("Test" is the element 1,1 and "Maximum acceleration" is the element 1,2), then the other rows are made by the name of the test (first column) and a number (second column). Thank you!
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 el 10 de Oct. de 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.

Iniciar sesión para comentar.

 Respuesta aceptada

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 comentario

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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by