A few days ago I posted this question and got the following splendid solution:
fid = fopen('C:\Testes_veloc\test.txt', 'Wt');
fmt = sprintf('%s;%d;%%d;%d;%d;%%d;%%d;%%d;%%.4f \\n',str1,num1,0,2)
[a,b,c,d] = ndgrid(vect2,vect1,day,1:15);
out = sprintf(fmt, [d(:), c(:), b(:), a(:), reshape(permute(MD,[2,1,3,4]),[],1)]');
fprintf(fid, '%s', out);
The variables str1
, num1
, day
, vect1
, vect2
and MD
are inputs of this function:
str1
is a string 1x1num1
is an integer 1x1day
is a vector 10x1vect1
is a vector 7x1vect2
is a vector 180x1MD
is a 4D matrix (7x180x10x15)
The objective was to have a text file as follows:
result.txt:
RED;12;7;0;2;1;4;7;0.0140
RED;12;7;0;2;2;9;7;0.1484
RED;12;7;0;2;3;7;4;0.1787
RED;12;7;0;2;4;2;6;0.7891
RED;12;7;0;2;5;9;6;0.1160
RED;12;7;0;2;6;9;1;0.9893
However, str1
is not a 1x1 string; it's a vector of names (189000x1), that has the length of the text that I desire. In other words, instead of only 'RED', I have many different others strings. Is it possible to adjust this vectorized loop to this situation?
I tried this (add the str1(:)
to the concatenation part), but unsuccessfully:
fmt = sprintf('%%s;%s;%d;%%d;%d;%d;%%d;%%d;%%d;%%.4f \\n',str1,num1,0,2)
out = sprintf(fmt, [str1 (:), d(:), c(:), b(:), a(:), reshape(permute(MD,[2,1,3,4]),[],1)]');
For example, str(1,:) = 'RED'
; str(2,:) = 'FHAW'
; str(3,:) = 'KI81'
; a cell like this.
It fails to concatenate the string to the numbers. Does anyone have solution?
Thanks in advance.