Changing array type?
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Marc-Olivier Labrecque-Goulet
am 7 Mär. 2017
Bearbeitet: Jan
am 7 Mär. 2017
Hi I'm trying to convert some data in an array : tab = [1 2 3 4 5; 1 10 11 100 101];
into something like this :
tab = {1 2 3 4 5 ; '001' '010' '011' '100' '101'};
I think I've got almost everything figure out except how I can transform my array from : tab = [1 2 3 4 5; 1 10 11 100 101]; to : tab = {1 2 3 4 5; 1 10 11 100 101};
0 Kommentare
Akzeptierte Antwort
Jan
am 7 Mär. 2017
Bearbeitet: Jan
am 7 Mär. 2017
To get from tab = [1 2 3 4 5; 1 10 11 100 101]; to : tab = {1 2 3 4 5; 1 10 11 100 101} :
tab = [1 2 3 4 5; 1 10 11 100 101];
tabc = num2cell(tab);
But the more complicated problem is the conversion to strings of the 2nd row. With the undocumented sprintfc command:
tab = [1 2 3 4 5; 1 10 11 100 101];
tabc = cat(1, num2cell(tab(1, :)), sprintfc('%03d', tab(2, :)))
sprintfc exists for many years now, but it is not mentioned in the docs, such that it might be removed in the future. To be bullet-proof, use a loop:
len = size(tab, 2);
cstr = cell(1, len);
for k = 1:len
cstr{k} = sprintf('%03d', tab(2, k));
end
tabc = cat(1, num2cell(tab(1, :)), cstr);
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices 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!