fprintf vector with variable size

조회 수: 10 (최근 30일)
Isha Sharma
Isha Sharma 2018년 3월 27일
댓글: Walter Roberson 2018년 3월 28일
I want to display values of variable in the code while the code is running. Problem is that size of one of the variable is not fixed. For example it is a vector and can be 3x1 or 6x1 or 9x1. I am not sure how I can use fprintf() for this. For example, k1=2, k2=45, a_det = [3;5;7]
or k1=3, k2=34, a_det = [2;5;7;9;4]
This is what I have:
fid = fopen('ADMM_Release.csv','wt');
for k1 = 1:1000
for k2 = 1:200
fprintf(fid, '%12.6f %12.6f %12.6f %12.6f %12.6f %12.6f %12.6f\n', k1, k2, time_Release_MIQP, rho, a_det ) ;
end
end
fclose(fileID);
  댓글 수: 2
Sullivan Hidot
Sullivan Hidot 2018년 3월 28일
Hi Isha, To deal with the various size of a variable, you have to convert 'a_det' into string then remove brackets and replace ';' by spaces. For example :
a=4;
b=5;
a_det=[1;2;3];
fprintf('%d %d %s\n', a, b, replace(regexprep(mat2str(a_det), '[[|]]', ''), ';', ' '))
Hope this helps
Walter Roberson
Walter Roberson 2018년 3월 28일
This appears to duplicate a different Question, which was answered at https://www.mathworks.com/matlabcentral/answers/390839-fprintf-to-display-variables#comment_550367
The solution has nothing to do with regexprep

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by