add space column to matrix

12 visualizzazioni (ultimi 30 giorni)
Keren Grinberg
Keren Grinberg il 6 Mar 2022
Risposto: DGM il 6 Mar 2022
i need to write *.inp file,
i have 2 matrix that i want to conbine:
A=[1 2 3;4 5 6]
and matrix i created from vectorL
b=['a' 'b' 'c' 'd']
B=repelem(b,2,1)
C=[1 2 3 ;'a' 'b' 'c' 'd';4 5 6 ;'s' 's' 's' 's']
after 3 and 6 i need space string\element.
I did this code:
C is great, but the inp file isnt
1, 2, 3, NaN
NaN, NaN, NaN, NaN
4, 5, 6, NaN
NaN, NaN, NaN, NaN
the most importent thing is that i need spaces charecters instead of 'NaN' in the file
thanks!!
A=[1 2 3;4 5 6]
b=["a", 'b', 'c', 'd'];
B=repelem(b,2,1)
nans=nan(2,1)
a=[A nans];
aB=[a B]'
C=reshape(aB,4,4)'
fileID=fopen('AB.inp','w')
fprintf(fileID,'\n%G, %G, %G, %G',C')
fclose(fileID);
type AB.inp

Risposte (1)

DGM
DGM il 6 Mar 2022
See if this is more along the lines of what you need
A = [1 2 3;4 5 6]
b = {'a' 'b' 'c' 'd'}
Ac = split(sprintf('%G\n',A')) % convert numeric to cellchar
Ac = reshape(Ac(1:end-1),size(A)) % reshape
Ac = [Ac {' '; ' '}]; % append spaces
C = [Ac(1,:); b; Ac(2,:); b].' % concatenate and transpose
fileID=fopen('AB.inp','w');
fprintf(fileID,'\n%s, %s, %s, %s',C{:});
fclose(fileID);

Categorie

Scopri di più su Cell Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by