Filter löschen
Filter löschen

Write a simple txt files made by strings and numbers

1 Ansicht (letzte 30 Tage)
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 Kommentare
J. Alex Lee
J. Alex Lee am 10 Okt. 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
Adam Danz am 10 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

J. Alex Lee
J. Alex Lee am 10 Okt. 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')
  1 Kommentar
Guglielmo Giambartolomei
Guglielmo Giambartolomei am 10 Okt. 2019
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by