fprintf vector with variable size
12 次查看(过去 30 天)
显示 更早的评论
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
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
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 Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!