fprintf vector with variable size

11 Ansichten (letzte 30 Tage)
Isha Sharma
Isha Sharma am 27 Mär. 2018
Kommentiert: Walter Roberson am 28 Mär. 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 Kommentare
Sullivan Hidot
Sullivan Hidot am 28 Mär. 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 am 28 Mär. 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

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Environment and Settings 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