# Write S2P Touchstone Files

This example shows how to write the data in a `circuit` object created in the MATLAB® workspace into an industry-standard data file, Touchstone®. You can use these files in third-party tools.

To write a touchstone file, in this example an RLGC transmission line object is created and analyzed in the frequency domain. This analyzed results are written into a Touchstone file and the data is compared with the original result.

### Create RF Circuit Object to Represent an RLCG Transmission Line

Create a `txlineRLCGLine` object to represent a RLCG transmission line. This example uses Name-Value pairs to implement the parameters in the RLCG transmission line shown in figure 1 [1].

Figure 1: RLCG transmission line.

`ckt1 = txlineRLCGLine('R',100,'L',80e-9,'C',200e-12,'G',1.6);`

### Clone Circuit Object

Use the `clone` function to make a copy of the transmission line object.

`ckt2 = clone(ckt1)`
```ckt2 = txlineRLCGLine: RLCGLine element Name: 'RLCGLine' Frequency: 1.0000e+09 R: 100 L: 8.0000e-08 C: 2.0000e-10 G: 1.6000 IntpType: 'Linear' LineLength: 0.0100 Termination: 'NotApplicable' StubMode: 'NotAStub' NumPorts: 2 Terminals: {'p1+' 'p2+' 'p1-' 'p2-'} ```

### Cascade Two Circuit Objects

Use the `circuit` object to cascade the two transmission lines.

`ckt = circuit([ckt1,ckt2]);`

### Analyze and Plot S-Parameter Data

Use the `sparameters` object to analyze the cascadeed transmission line in the frequency domain.

```freq = linspace(0,10e9); ckt_sparameters = sparameters(ckt,freq);```

Use the `smithplot` method to plot the object's `S11` on a Smith chart®.

```figure smithplot(ckt_sparameters,[1,1],'LegendLabels','S11 Original')```

### Write Data to S2P File

Use the `rfwrite` function to write the data to a file.

```workingdir = tempname; mkdir(workingdir); filename = fullfile(workingdir,'myrlcg.s2p'); if exist(filename,'file') delete(filename) end rfwrite(ckt_sparameters,filename);```

### Compare Data

Read the data from the file `myrlcg.s2p` into a new `sparameters` object and plot input reflection coefficient, `S11` on a Smith chart. Visually compare the 'S11 original' and 'S11 from S2P' to confirm that the data matches.

```compare_ckt = sparameters(filename); figure smithplot(compare_ckt,[1,1],'LegendLabels','S11 from S2P')```

[1] M. Steer, "Transmission Lines," in Microwave and RF Design: Transmission Lines. vol. 2, 3rd ed. Raleigh, North Carolina, US: North Carolina State University, 2019, ch. 2, sec. 2, pp.58.