1
\$\begingroup\$

So I am doing a Monte Carlo Simulation on LTSpice. Then I want to export my data and manipulate it in MATLAB.

Here is a working example file, just with 2 decades each one with 11 points and 6 iterations of Monte Carlo:

Freq.   V(vout)
Step Information: X=0  (Run: 1/6)
1.00000000000000e-002   (-6.26777881841443e+000dB,-2.45632080921360e+001°)
1.25892541179417e-002   (-6.36861181114449e+000dB,-3.06433832660851e+001°)
1.58489319246111e-002   (-6.50874601655850e+000dB,-3.80698128918313e+001°)
1.99526231496888e-002   (-6.68840252431672e+000dB,-4.70502691728284e+001°)
2.51188643150958e-002   (-6.88767600648017e+000dB,-5.78295465358373e+001°)
3.16227766016838e-002   (-7.04662306199209e+000dB,-7.08121145050537e+001°)
3.98107170553497e-002   (-7.04613385608013e+000dB,-8.69037556678446e+001°)
5.01187233627273e-002   (-6.73302774870351e+000dB,-1.08291925528466e+002°)
6.30957344480194e-002   (-6.17898060206205e+000dB,-1.39490624870074e+002°)
7.94328234724282e-002   (-6.29156027893901e+000dB,1.76371893719709e+002°)
1.00000000000000e-001   (-7.00343610611535e+000dB,1.25917428931445e+002°)
1.25892541179417e-001   (-6.07744581972252e+000dB,4.88363107428150e+001°)
1.58489319246111e-001   (-6.92190787935448e+000dB,-1.05087106512531e+002°)
1.99526231496888e-001   (-3.64476579715909e+001dB,1.50248213268233e+002°)
2.51188643150958e-001   (-5.65563441261771e+001dB,1.30679615367055e+002°)
3.16227766016838e-001   (-7.35748460393752e+001dB,1.19998987277993e+002°)
3.98107170553498e-001   (-8.92621053142513e+001dB,1.12887963014667e+002°)
5.01187233627273e-001   (-1.04257065089212e+002dB,1.07760106447624e+002°)
6.30957344480194e-001   (-1.18860319936777e+002dB,1.03910331684806e+002°)
7.94328234724282e-001   (-1.33231869456083e+002dB,1.00954550855709e+002°)
1.00000000000000e+000   (-1.47462843245236e+002dB,9.86551571694855e+001°)
Step Information: X=1  (Run: 2/6)
1.00000000000000e-002   (-6.16672256403144e+000dB,-2.44850335449979e+001°)
1.25892541179417e-002   (-6.26639121158446e+000dB,-3.05477574094463e+001°)
1.58489319246111e-002   (-6.40485695767013e+000dB,-3.79544592941995e+001°)
1.99526231496888e-002   (-6.58222673567182e+000dB,-4.69137543618026e+001°)
2.51188643150958e-002   (-6.77853960525347e+000dB,-5.76720745778276e+001°)
3.16227766016838e-002   (-6.93392757217094e+000dB,-7.06368382452438e+001°)
3.98107170553497e-002   (-6.92972272669119e+000dB,-8.67198835897928e+001°)
5.01187233627273e-002   (-6.61488302611489e+000dB,-1.08121876478984e+002°)
6.30957344480194e-002   (-6.07070535374054e+000dB,-1.39362050183132e+002°)
7.94328234724282e-002   (-6.21965984211102e+000dB,1.76590682774648e+002°)
1.00000000000000e-001   (-6.95327975730355e+000dB,1.26605975615689e+002°)
1.25892541179417e-001   (-5.98464834532776e+000dB,4.97190587873480e+001°)
1.58489319246111e-001   (-6.40417682367860e+000dB,-9.75145351869804e+001°)
1.99526231496888e-001   (-3.60323515588510e+001dB,1.49599381516468e+002°)
2.51188643150958e-001   (-5.62806798193614e+001dB,1.30188107624632e+002°)
3.16227766016838e-001   (-7.33429304420458e+001dB,1.19642264129678e+002°)
3.98107170553498e-001   (-8.90496342710569e+001dB,1.12621087528093e+002°)
5.01187233627273e-001   (-1.04054663427162e+002dB,1.07555972526800e+002°)
6.30957344480194e-001   (-1.18663568970525e+002dB,1.03751971633780e+002°)
7.94328234724282e-001   (-1.33038438305737e+002dB,1.00830612529090e+002°)
1.00000000000000e+000   (-1.47271416461901e+002dB,9.85576222906644e+001°)
Step Information: X=2  (Run: 3/6)
1.00000000000000e-002   (-6.21225990393714e+000dB,-2.42398271464396e+001°)
1.25892541179417e-002   (-6.30909308389625e+000dB,-3.02474562172609e+001°)
1.58489319246111e-002   (-6.44368675411592e+000dB,-3.75911929919443e+001°)
1.99526231496888e-002   (-6.61617014498940e+000dB,-4.64809809162114e+001°)
2.51188643150958e-002   (-6.80702103232767e+000dB,-5.71648578399885e+001°)
3.16227766016838e-002   (-6.95744340949862e+000dB,-7.00503088335492e+001°)
3.98107170553497e-002   (-6.95058694718676e+000dB,-8.60451477040284e+001°)
5.01187233627273e-002   (-6.63826350750918e+000dB,-1.07340680147192e+002°)
6.30957344480194e-002   (-6.10854527731718e+000dB,-1.38439087101438e+002°)
7.94328234724282e-002   (-6.30362830475549e+000dB,1.77802087261242e+002°)
1.00000000000000e-001   (-7.12172801096167e+000dB,1.28771964802357e+002°)
1.25892541179417e-001   (-6.03607855680839e+000dB,5.40055044059610e+001°)
1.58489319246111e-001   (-6.09076329776158e+000dB,-7.96416809130710e+001°)
1.99526231496888e-001   (-3.50723893849773e+001dB,1.49324548669222e+002°)
2.51188643150958e-001   (-5.56565653180954e+001dB,1.29763151240751e+002°)
3.16227766016838e-001   (-7.28297203350366e+001dB,1.19299898524730e+002°)
3.98107170553498e-001   (-8.85876592266688e+001dB,1.12354271502988e+002°)
5.01187233627273e-001   (-1.03619895627022e+002dB,1.07347693711844e+002°)
6.30957344480194e-001   (-1.18244315768821e+002dB,1.03588572559094e+002°)
7.94328234724282e-001   (-1.32628393065329e+002dB,1.00701890827225e+002°)
1.00000000000000e+000   (-1.46866966442567e+002dB,9.84559235597901e+001°)
Step Information: X=3  (Run: 4/6)
1.00000000000000e-002   (-6.22331279803977e+000dB,-2.46332673587796e+001°)
1.25892541179417e-002   (-6.32504917213284e+000dB,-3.07272672560921e+001°)
1.58489319246111e-002   (-6.46623144544338e+000dB,-3.81680557698355e+001°)
1.99526231496888e-002   (-6.64677864154134e+000dB,-4.71624573226846e+001°)
2.51188643150958e-002   (-6.84606579338628e+000dB,-5.79553475142998e+001°)
3.16227766016838e-002   (-7.00283755937839e+000dB,-7.09556477273628e+001°)
3.98107170553497e-002   (-6.99627249573579e+000dB,-8.70859007405126e+001°)
5.01187233627273e-002   (-6.67343602866177e+000dB,-1.08579125550146e+002°)
6.30957344480194e-002   (-6.12030688950781e+000dB,-1.40015647535145e+002°)
7.94328234724282e-002   (-6.27478190951681e+000dB,1.75681713302692e+002°)
1.00000000000000e-001   (-6.99206525364281e+000dB,1.25430995493748e+002°)
1.25892541179417e-001   (-6.03691617350726e+000dB,4.75748098362317e+001°)
1.58489319246111e-001   (-6.78167767121785e+000dB,-1.05705064001918e+002°)
1.99526231496888e-001   (-3.66017286534973e+001dB,1.49104719709746e+002°)
2.51188643150958e-001   (-5.67225468275595e+001dB,1.29982261243041e+002°)
3.16227766016838e-001   (-7.37401305848446e+001dB,1.19514081606539e+002°)
3.98107170553498e-001   (-8.94256387901318e+001dB,1.12530467253416e+002°)
5.01187233627273e-001   (-1.04419241358117e+002dB,1.07488355447641e+002°)
6.30957344480194e-001   (-1.19021571859160e+002dB,1.03700150338241e+002°)
7.94328234724282e-001   (-1.33392517159571e+002dB,1.00790315794714e+002°)
1.00000000000000e+000   (-1.47623102632076e+002dB,9.85260246778205e+001°)
Step Information: X=4  (Run: 5/6)
1.00000000000000e-002   (-6.23268747704643e+000dB,-2.50410800230128e+001°)
1.25892541179417e-002   (-6.33917575843080e+000dB,-3.12217854974529e+001°)
1.58489319246111e-002   (-6.48629042886293e+000dB,-3.87581607866792e+001°)
1.99526231496888e-002   (-6.67304499854865e+000dB,-4.78538422903985e+001°)
2.51188643150958e-002   (-6.87639806629557e+000dB,-5.87536116429439e+001°)
3.16227766016838e-002   (-7.03051544993454e+000dB,-7.18804940213071e+001°)
3.98107170553497e-002   (-7.00828150775649e+000dB,-8.82104246417903e+001°)
5.01187233627273e-002   (-6.65530648891537e+000dB,-1.10119730420866e+002°)
6.30957344480194e-002   (-6.09469920219503e+000dB,-1.42405205514390e+002°)
7.94328234724282e-002   (-6.34016880658946e+000dB,1.72505719420889e+002°)
1.00000000000000e-001   (-7.04017166605841e+000dB,1.21990674426408e+002°)
1.25892541179417e-001   (-6.07437235545994e+000dB,4.12337724162087e+001°)
1.58489319246111e-001   (-8.18869568325135e+000dB,-1.25105992577795e+002°)
1.99526231496888e-001   (-3.80083248976535e+001dB,1.47144346294457e+002°)
2.51188643150958e-001   (-5.78497981809793e+001dB,1.28956714532505e+002°)
3.16227766016838e-001   (-7.47637582348090e+001dB,1.18825272338581e+002°)
3.98107170553498e-001   (-9.03986136722447e+001dB,1.12028380207313e+002°)
5.01187233627273e-001   (-1.05364437014045e+002dB,1.07108394410606e+002°)
6.30957344480194e-001   (-1.19950616999652e+002dB,1.03406857883888e+002°)
7.94328234724282e-001   (-1.34311862227457e+002dB,1.00561359030665e+002°)
1.00000000000000e+000   (-1.48536509737044e+002dB,9.83460945944903e+001°)
Step Information: X=5  (Run: 6/6)
1.00000000000000e-002   (-6.25082744156451e+000dB,-2.47181947915387e+001°)
1.25892541179417e-002   (-6.35355099949484e+000dB,-3.08297344200067e+001°)
1.58489319246111e-002   (-6.49590256038996e+000dB,-3.82894691652311e+001°)
1.99526231496888e-002   (-6.67751099794831e+000dB,-4.73034739546178e+001°)
2.51188643150958e-002   (-6.87704700139995e+000dB,-5.81169846727973e+001°)
3.16227766016838e-002   (-7.03196827194024e+000dB,-7.11437980905331e+001°)
3.98107170553497e-002   (-7.01979302428447e+000dB,-8.73238848974908e+001°)
5.01187233627273e-002   (-6.68796414576871e+000dB,-1.08936886014197e+002°)
6.30957344480194e-002   (-6.13713289323238e+000dB,-1.40629994073302e+002°)
7.94328234724282e-002   (-6.33523944865220e+000dB,1.74880767731570e+002°)
1.00000000000000e-001   (-7.06204694631925e+000dB,1.24795188737801e+002°)
1.25892541179417e-001   (-6.07176510225266e+000dB,4.62141099523223e+001°)
1.58489319246111e-001   (-6.94683355469541e+000dB,-1.07596087699931e+002°)
1.99526231496888e-001   (-3.68700051939526e+001dB,1.48337323013288e+002°)
2.51188643150958e-001   (-5.69761930020967e+001dB,1.29501884676301e+002°)
3.16227766016838e-001   (-7.39858496501359e+001dB,1.19174614947729e+002°)
3.98107170553498e-001   (-8.96667862090866e+001dB,1.12277917628911e+002°)
5.01187233627273e-001   (-1.04657656100839e+002dB,1.07295362073189e+002°)
6.30957344480194e-001   (-1.19258318601153e+002dB,1.03550408363673e+002°)
7.94328234724282e-001   (-1.33628232853353e+002dB,1.00673080554496e+002°)
1.00000000000000e+000   (-1.47858176057854e+002dB,9.84337357907819e+001°)

So I already have a code that works with in case of a plot with just 1 iteration (it doesn't have the "Step Information: X=0 (Run: 1/6)" line . I've adapted this from another code I've seen.

clc; 
close all; 
clear all; 

filename = 'FrequencyResponse.txt';
filetoread = fopen(filename, 'rt');
Dummy= textscan(filetoread, 'Freq.  V(vout)', 'CollectOutput', 1);
DataScanned = textscan(filetoread, '%f(%fdB,%f°)', 'CollectOutput',1);
fclose(filetoread);
D = cell2mat(DataScanned);
D(:,1)=D(:,1)*2*pi;
figure
subplot(2,1,1)
semilogx(D(:,1), D(:,2))
title('Amplitude (dB)')
grid
subplot(2,1,2)
semilogx(D(:,1), D(:,3))
title('Phase (°)')
grid
xlabel('Frequency')

So yeah this code works for just a normal simulation. Now for Monte Carlo there are a lot of challenges that I am facing. For instance:

1 - How should I read the "Step Information yada yada" and then read all the values and then stop at the next step information.

2 - How to read the number of iterations?

3 - How should I store the info of each interation? I'm thinking of a matrix, each column for each Monte Carlo iteration (frequency, voltage magnitude, voltage phase). But I already have a 3 column matrix...

Some help would be appreciated. I have little experience with MATLAB and I always struggled with reading files in C. So this is really confusing for me.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I don't know anything about MATLAB. Never used it. (Money I won't shell out.) But it basically looked to me like you are stepping through a PARAM variable X from 0 to some limit and using that to modify things, generating a Bode plot each time. I don't know what you want to do in MATLAB, exactly. But I think you should work out exactly what you intend on doing with each Bode plot vector (each value is a 2-vector of magnitude and phase) and then read things up and do it. This should be something you can work out on paper, before coding, pretty easily. \$\endgroup\$
    – jonk
    Commented Dec 2, 2021 at 21:25

1 Answer 1

4
\$\begingroup\$

Something along the lines of this:

fn = "test_dat.txt";   % Filename variable - no trailing carriage return
S = readlines(fn); % read all lines into an array 

STEP=0; % initialise counter 
results = struct.empty; % initialise result structure 
rx = '[+-.e0-9]*' % create regex pattern to match exponent numbers

for i=2:length(S) % loop over every line BUT skip the 1st
    if startsWith(S(i),'Step') % check if the line starts with the word "step", if so use this as a marker for a new step group 
        STEP = STEP+1; % increment the STEP counter
        results(STEP).data = [0 0 0]; % seed a new results array
    else
        num = str2double(regexp(S(i),rx,'match')) % match the three exponential numbers in a line based upon regex pattern
        results(STEP).data = [results(STEP).data ; num(1) num(2) num(3)]; % create an row and append to the results table associated with this STEP
    end
end

figure;  % create a figure
subplot(2,1,1); % select 1st subplot
ylabel('Amplitude (dB)'); % set y label
xlabel('Frequency'); % set xlabel
set(gca, 'XScale', 'log'); % change xaxis to log
hold; % hold plot to permit multiple plots in one axis
grid; % turn on grid
subplot(2,1,2) % same steps for 2nd subplot
ylabel('Phase (°)')
xlabel('Frequency')
set(gca, 'XScale', 'log');
hold;
grid;

for i = 1:numel(results) % start looping over all entries in the results struct
    subplot(2,1,1) % select upper subplot 
    plot(results(i).data(:,1)*2*pi, results(i).data(:,2)); % plot freq (1st column) and mag (2nd column)
    subplot(2,1,2) % select lower subplot
    plot(results(i).data(:,1)*2*pi, results(i).data(:,3)); % plot freq (1st column) and angle (3rd)
end

enter image description here

\$\endgroup\$
5
  • \$\begingroup\$ Hi Jon! Thank you for your answer. I've tried to run your code (changing the name to the one of my file of course) but get the following error: Index exceeds the number of array elements (0). Error in Graph_Script (line 17) results(STEP).data = [results(STEP).data ; num(1) num(2) num(3)]; Any idea why? Also I don't quite understand what you are doing with the rx line. Frankly I am having a lot of trouble understanding the meaning of everything, as I said I'm at a very beginning level in MATLAB. But thank you for your help! \$\endgroup\$ Commented Dec 2, 2021 at 23:40
  • \$\begingroup\$ what it will be is there is a blank line at the end of the file - I was too lazy to put a check in so if you ensure there is no trailing carriage return that should fix it. The rx is setting up a regex pattern to basically extract part of a line that contain those characters. \$\endgroup\$
    – user16222
    Commented Dec 2, 2021 at 23:45
  • \$\begingroup\$ Oh ok! Since the line is always present, I will just go until length(S)-1. I will try and give feedback, thank you very much for your help! \$\endgroup\$ Commented Dec 2, 2021 at 23:52
  • 1
    \$\begingroup\$ or something like: if length(num) != 3 continue else results(STEP).data = .... \$\endgroup\$
    – user16222
    Commented Dec 2, 2021 at 23:52
  • \$\begingroup\$ Ok, thank you and thank you for also commenting the code, I will check it again tomorrow and if any new questions arise I'll comment here again. Thank you for your help, really! \$\endgroup\$ Commented Dec 3, 2021 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.