LarrySnyder610
LarrySnyder610

Reputation: 2397

MATLAB avoid matrix wrapping in command window

Is there a way to prevent MATLAB from wrapping matrices into multiple chunks when displaying them in the command window? Here's what I mean:

>> x = rand(10,1);
>> y = rand(10,1);
>> c = squareform(pdist([x y]))

c =

  Columns 1 through 6

         0    0.9160    0.4707    0.7161    0.6093    0.1555
    0.9160         0    0.8495    0.8984    0.6463    1.0714
    0.4707    0.8495         0    0.2459    0.2477    0.5541
    0.7161    0.8984    0.2459         0    0.2603    0.7970
    0.6093    0.6463    0.2477    0.2603         0    0.7306
    0.1555    1.0714    0.5541    0.7970    0.7306         0
    0.0881    0.9695    0.4311    0.6762    0.6012    0.1295
    0.4698    0.4566    0.4587    0.6057    0.3612    0.6245
    0.2442    1.1079    0.7006    0.9460    0.8534    0.1629
    0.8282    0.1355    0.7200    0.7629    0.5114    0.9832

  Columns 7 through 10

    0.0881    0.4698    0.2442    0.8282
    0.9695    0.4566    1.1079    0.1355
    0.4311    0.4587    0.7006    0.7200
    0.6762    0.6057    0.9460    0.7629
    0.6012    0.3612    0.8534    0.5114
    0.1295    0.6245    0.1629    0.9832
         0    0.5156    0.2700    0.8736
    0.5156         0    0.6857    0.3588
    0.2700    0.6857         0    1.0359
    0.8736    0.3588    1.0359         0

I'd like to be able to copy and paste the matrix c (into a LaTeX document, say, or a MATLAB script) but this is obviously cumbersome with the current output format, especially for larger matrices.

Upvotes: 0

Views: 445

Answers (2)

sco1
sco1

Reputation: 12214

As I mentioned in my comment, I don't think there's a way to change the command line output. If you don't need a programmatic solution you can utilize the variable explorer to interact with your data using a slightly Excel-ish interface.

You can access the variable explorer by double clicking on your variable in the workspace browser, right clicking on your variable and selecting Open, selecting your variable and hitting ctrl+D (on Windows), or programmatically using openvar.

If you do need a programmatic solution, you can use one of the many exporting functions (sprintf, fprintf, save, etc.), one example being the answer that @badjr posted.

Upvotes: 2

badjr
badjr

Reputation: 2286

You could do fprintf([repmat('%f\t', 1, size(c, 2)) '\n'], c');, which gave this output:

0.000000    0.818064    1.054641    0.342287    0.668041    0.717356    0.597756    0.804045    0.650459    0.815819    
0.818064    0.000000    0.778921    0.485276    0.322136    1.157594    0.833495    0.363079    0.185730    0.060130    
1.054641    0.778921    0.000000    0.917058    0.529164    0.815812    0.556431    0.421934    0.846744    0.837905    
0.342287    0.485276    0.917058    0.000000    0.422061    0.885196    0.638057    0.565268    0.309989    0.476907    
0.668041    0.322136    0.529164    0.422061    0.000000    0.848242    0.518164    0.143653    0.325248    0.368679    
0.717356    1.157594    0.815812    0.885196    0.848242    0.000000    0.333280    0.894846    1.078174    1.191962    
0.597756    0.833495    0.556431    0.638057    0.518164    0.333280    0.000000    0.562174    0.773488    0.871944    
0.804045    0.363079    0.421934    0.565268    0.143653    0.894846    0.562174    0.000000    0.428803    0.420291    
0.650459    0.185730    0.846744    0.309989    0.325248    1.078174    0.773488    0.428803    0.000000    0.167448    
0.815819    0.060130    0.837905    0.476907    0.368679    1.191962    0.871944    0.420291    0.167448    0.000000    

But it's probably easier to use the variable explorer as mentioned in the comments.

Upvotes: 5

Related Questions