fprintf vector with variable size

14 views (last 30 days)
Isha Sharma
Isha Sharma on 27 Mar 2018
Commented: Walter Roberson on 28 Mar 2018
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 Comments
Sullivan Hidot
Sullivan Hidot on 28 Mar 2018
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 on 28 Mar 2018
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

Sign in to comment.

Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!