MATLAB Answers

plotting separate parts of a column on different figures

2 views (last 30 days)
I have a large matrix. In the 3rd column, I have my "eta" values. In my 4th column, I have "r" values. In my 8th column, I have "g" values.
Eta is the same as you go down column for a while. How can I plot g vs. r as long as eta is the same, then make a new plot with g vs r anytime eta changes?
Maybe the matrix needs to be reshaped first? Note that the number of eta values that are the same can change.

  6 Comments

Show 3 older comments
Benjamin
Benjamin on 27 Mar 2019
But all of them are different sizes though, they can't easily go into an array. I'm not sure how to better import the data. How could I load all of this in a struct?
The alternative is I just import the sheet name "Master" and then I have all the data. Then I have the problem that I initially asked in this question. By loading them in separately, I don't have to separate them in MATLAB.
You can see the sheet names. Can I load these in a better way? How do I load all of them to avoid this mess of a code?
Stephen Cobeldick
Stephen Cobeldick on 27 Mar 2019
"But all of them are different sizes though, they can't easily go into an array"
That is exactly what cell arrays are for.
"The alternative is I just import the sheet name "Master" and then I have all the data."
That would likely be the best solution.
"Then I have the problem that I initially asked in this question. By loading them in separately, I don't have to separate them in MATLAB."
Processing the data within MATLAB is much more efficient than importing data from file multiple times (hard-drives are very slow!). Simply import that data all at once, use diff to detect where eta changes, then loop over those indices and plot the data.
Benjamin
Benjamin on 27 Mar 2019
I am not sure how to use diff to detect eta changes and plot appropriately. Any chance you could show me what you mean?

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 27 Mar 2019
Edited: Stephen Cobeldick on 27 Mar 2019
Following your description "I have a large matrix. In the 3rd column, I have my "eta" values. In my 4th column, I have "r" values. In my 8th column, I have "g" values", here is one simple loop:
% Random fake data with contiguous eta groups:
mat = rand(32,8);
mat(:,3) = sort(randi(9,32,1));
% Detect eta groups (i.e. changes in eta):
bnd = find([true;diff(mat(:,3));true]);
% Plot data for each eta group:
for k = 1:numel(bnd)-1
figure() % using ONE figure is usually simpler.
idx = bnd(k):bnd(k+1)-1;
G = mat(idx,8);
R = mat(idx,4);
plot(R,G) % or plot(G,R), whatever you want
title(sprintf('eta = %d',mat(bnd(k),3)))
end

  8 Comments

Show 5 older comments
Stephen Cobeldick
Stephen Cobeldick on 27 Mar 2019
"How do I plot this equation without looping through the r's?"
Why not just use the R values, which are defined on this line:
R = mat(idx,4);
You do NOT need a loop, read the link I gave you earlier about vectorized code (and make the appropriate changes to your formula, e.g. replace
^
with
.^
As I wrote before, most likely you could simplify the entire "formula" by vectorizing the coefficients as well. But for a start atleast vectorize it for the R values.
Benjamin
Benjamin on 28 Mar 2019
So I took your advice, removed the loop and vectorized it with r.^. Seems to work well. One issue that I get when I replace r with R is that the vectors must be the same length. The number of R values changes on each eta value. Is there a way to circumvent this?
Stephen Cobeldick
Stephen Cobeldick on 28 Mar 2019
"The number of R values changes on each eta value."
It is not clear to me what this means.
"Is there a way to circumvent this?"
As far a I can tell you could use the dimensions of your arrays. For example, if R is a column vector and eta a row vector, then they can be different lengths and you wll get all values at the output:
>> eta = [1,2,3]; % row
>> R = [4;5;6;7]; % column
>> R.^eta
ans =
4 16 64
5 25 125
6 36 216
7 49 343
Use higher dimensions if required: e.g. if R is an MxN matrix that you do not want to reshape, then reshape eta to a 1x1xP vector, it works in exactly the same way.
Using the dimensions ot arrays is one of MATLAB's most powerful features.
For MATLAB versions prior to R2016b you will need to use bsxfun.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by