필터 지우기
필터 지우기

Write a simple txt files made by strings and numbers

조회 수: 1 (최근 30일)
Guglielmo Giambartolomei
Guglielmo Giambartolomei 2019년 10월 9일
댓글: Guglielmo Giambartolomei 2019년 10월 10일
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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by