data = [Angle Velocity Distance Acceleration]';
...
where not all vectors are of same length.
Doesn't seem to make much sense if the missing values of the shorter observations are scattered, so simply augment the shorter with NaN or some other indicator value based on length of longest before the concatenation operation.
Or, of course, while a little more work, you could loop through for 1:max(lengths) and write row by row testing for whether there is value for each variable and using a count-specific fprintf argument list and a fill string for the missing column(s). Obviously, the first solution is simpler.
Of course, whether the result makes any sense or not is another issue... :)
ADDENDUM
OK, the example given would have to be a cell array to be concatenated and fprintf isn't able to handle cell arrays as arrays any way. You will have to make sizes commensurate with missing value indicators if you insist on the concatenation route.
If all were numeric as would be presumed from original variables names, then you could have two operations; concatenate the subsections of same size and fprintf them, then fprintf the subsection of longer elements as a second array.
But, for the mixed data you'll have to loop irregardless unless you were to import to a table (again will need missing value indicators, though). Then writetable might be able to deal with it for you although you'd have to convert the numerics to string inside the table methinks to be able to get the blank rows; missing values as NaN for numbers would still show up.
If were all numeric then something like
nR=[size(Angle,1) size(Velocity,1) size(Distance,1) size(Acceleration,1)];
dR=max(nR)-nR;
data=[Angle;nan(dR(1),1) Velocity;nan(dR(1),1) Distance;nan(dR(1),1) Acceleration;nan(dR(1),1)];
would fill the shorter columns with NaN to be able to concatenate. Of course, then the output would contain NaN which is probably not what want.
For the second example, it takes some more effort. If the four variables are cellstrings for the cities and numeric for the populations, then the following script is crude outline--
N=2;
fmt=[repmat('% 12s % 8d ',1,N) '\n'];
N1=min(length(Cities_Eur),length(Cities_US));
for i=1:N1
fprintf(fmt,char(Cities_Eur(i)),Pop_Eur(i),char(Cities_US(i)),Pop_USA(i))
end
N=1; fmt=[repmat('% 12s % 8d ',1,N) '\n'];
for i=N1+1:length(Cities_Eur)
fprintf(fmt,char(Cities_Eur(i)),Pop_Eur(i))
end
This yields
Paris 3000000 New York 5000000
Berlin 2500000 Los Angeles 4000000
London 6000000
Adding the header is another line first, of course.
Specialized output requires specialized coding for it, unfortunately...
Best Answer