2012-04-14 6 views
1

Dies wird auf die Post im ZusammenhangAnzeige alle Elemente in einer verschachtelten Zellenarray (mit ganzzahligen Einträge)

display all elements in a nested cell array (with character entries)

mit einer Änderung in dem Einträge Zeichen sind. Eine neue Frage wurde um Klarheit gebeten.

Jetzt:

a =

{1x10 cell} {1x10 cell} {1x10 cell} {1x10 cell} 

a {:} =

ans = [0] [0.4000] [0] [0] [0] [0] [0] [0] [0] [0] 

ans = [0] [0] [0.2000] [0] [0.2000] [0] [0.2000] [0] [0] [0] 

ans = [0] [0] [0] [0] [0] [0.2000] [0] [0] [0.2000] [0.2000] 

ans = [0] [0.2000] [0] [0] [0] [0] [0] [0.4000] [0] [0.2000] 

die Antwort auf die vorherige war:

fileID = fopen('a.txt', 'at'); 
fprintf(fileID, '%2.8s \n', cellfun(@(x) char(x), a)); 
fclose(fileID); 

Wie es jetzt lösen ? drucken möchten:

  0 0.4 0 0 0 0 0 0 0 0 
      0 0 0.2 0 0.2 0 0.2 0 0 0 
      . 
      . 

dank

Antwort

1

Dies ist eine Möglichkeit, es zu tun:

a = { { 0, 0.4000, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0.2000, 0, 0.2000, 0, 0.2000, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0.2000, 0, 0, 0.2000, 0.2000 }, { 0, 0.2000, 0, 0, 0, 0, 0, 0.4000, 0, 0.2000 }}; 

fileID = fopen('a.txt', 'at'); 

fprintf(fileID, [ (regexprep((regexprep((regexprep((regexprep(mat2str(cell2mat(cellfun(@cell2mat, a, 'UniformOutput', false)')), '(0)' , '$1 ')), '[', '')), ']', '')), ';', '\n')), '\n' ]); 

fclose(fileID); 

Edit: alternative Lösung. In diesem sind kürzere Linien mit Leerzeichen aufgefüllt.

CharMatrix = char(regexprep(cellfun(@mat2str, (cellfun(@cell2mat, a, 'UniformOutput', false)'), 'UniformOutput', false), '0 ', '0 ')); 
CharMatrix(CharMatrix == ']') = ' '; 
CharMatrix(:,1) = []; 
CharMatrix(:,end) = '\'; 
CharMatrix(:,end+1) = 'n'; 
fileID = fopen('a.txt', 'at'); 
fprintf(fileID, reshape(CharMatrix', 1, [])); 
fclose(fileID); 
+0

Dank @nrz. Gut, nur um all die obige Antwort zu würdigen, war auch richtig – pac

2

ich mich zu erinnern, wenn Sie die Werte in ein Array setzen, wird die Umwandlung in geeigneter Weise zu tun. Ich habe Matlab nicht, um es zu testen, aber das sollte funktionieren.

[a{:}] 
+0

danke @Pearsonartphoto – pac

Verwandte Themen