MATLAB Answers

0

Graphing Base Form of y=mx+b from a .csv file

Asked by Nathan Rivard on 15 Oct 2019 at 19:57
Latest activity Edited by Adam Danz
on 16 Oct 2019 at 17:11
I have a few thousand equations in y=mx+b form that I have acquired from hundreds of thousands of data points in multiple excel files. They are all trendlines of similar T-s data. I am attempting to create generalized trendlines of similar data types and I would like to be able to select formulas in y=mx+b form that I would like plotted, and then find the formula for the LOBF (Line of best fit) for that set of equations. I am new to MatLab and would just like some assistance in this.

  9 Comments

Adam Danz
on 15 Oct 2019 at 21:23
I don't have enough information to suggest how to read in the data. You could use Matlab's Import Data tool to start with. To use my solution below, you'll need to import the file as a cell array of strings or character arrays. You'll also need to get rid of the first column of numeric data since my answer expects that all elements of the cell array are strings.
If you get stuck, let me know where you're at.
The data was already imported through the import data tool and is sitting in a table of variables in MatLab. I got rid of the first column of numeric data as well but I will continue work onto tomorrow and post an update. Thank you for the help
Adam Danz
on 15 Oct 2019 at 21:35
That info is helpful. If you're reading it in as a table and you already got rid of the first column, the first line of my solution will be
numVals = regexp(data{:,:},'[+-]?\d+\.?\d*', 'match');
% ^^^^^ add this
Alternatively, if you hadn't removed the first column, you could have done,
numVals = regexp(data{:,2:end},'[+-]?\d+\.?\d*', 'match');
% ^^^^^^^^^
The rest of my solution will be the same.

Sign in to comment.

Products


Release

R2019b

1 Answer

Answer by Adam Danz
on 15 Oct 2019 at 21:20
 Accepted Answer

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})

  6 Comments

Adam Danz
on 16 Oct 2019 at 16:13
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.
Ok one more thing and I will 100% accept it. I would like the average of the slopes and the average of the intercepts seperately as none of the slopes are negative and they are all remotely close together
Adam Danz
on 16 Oct 2019 at 17:11
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(:));

Sign in to comment.