2 views (last 30 days)

Adam Danz
on 15 Oct 2019

I'm assuming you've read the data into Matlab and you've got a cell array of strings or character arrays named "data" that appears like this:

data =

3×6 cell array

Columns 1 through 3

{'y = 2.3297x + 1…'} {'y = 1.8866x + 1…'} {'y = 0.3373x + 1…'}

{'y = 1.9526x + 9…'} {'y = 1.7754x + 9…'} {'y = 2.774x + 68…'}

{'y = 1.9004x + 9…'} {'y = 1.9833x + 8…'} {'y = 3.1304x + 6…'}

Columns 4 through 6

{'y = 3.584x + 69…'} {'y = 2.1044x + 6…'} {'y = 0.0704x + 6…'}

{'y = 3.0042x + 6…'} {'y = 2.3015x + 5…'} {'y = 0.1415x + 6…'}

{'y = 3.1099x + 6…'} {'y = 2.466x + 60…'} {'y = 0.2486x + 6…'}

Execute these two lines below to extract the slope and intercept values from each string.

numVals = regexp(data,'[+-]?\d+\.?\d*', 'match');

mb = cellfun(@str2double, numVals, 'UniformOutput', false);

mb is a cell array the same size as "data" and contains the [slope,intercept] of each cell element.

mb =

3×6 cell array

{1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double}

{1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double}

{1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double}

For example, for the equation in data(2,3), the slope and intercept are

mb{2,3}

ans =

2.774 68.856

To draw a regression line on a plot use refline()

refline(mb{2,3})

Adam Danz
on 16 Oct 2019

Glad I can help!

There is no single regression line for 8 difference slopes and intercepts.

Do you mean you want to average the slopes and average the intercepts? Maybe compute the medians? Note that if you've got both positive and negative slopes, the mean slope could be a poor representation of the distribution of slopes.

Adam Danz
on 16 Oct 2019

Continuing from the mb cell array in my answer,

allSlopes = cellfun(@(x)x(1),mb); %matrix of all slopes in mb

allInt = cellfun(@(x)x(2),mb); %matrix of all intercepts in mb

meanSlope = mean(allSlopes(:));

meanYint = mean(allInt(:));

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 9 Comments

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756659

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756659

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756670

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756670

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756672

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756672

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756673

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756673

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756674

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756674

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756678

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756678

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756680

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756680

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756683

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756683

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756684

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/485532-graphing-base-form-of-y-mx-b-from-a-csv-file#comment_756684

Sign in to comment.