plotting separate parts of a column on different figures

8 ビュー (過去 30 日間)
Benjamin
Benjamin 2019 年 3 月 27 日
編集済み: Stephen23 2019 年 3 月 28 日
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 件のコメント
Stephen23
Stephen23 2019 年 3 月 27 日
編集済み: Stephen23 2019 年 3 月 27 日
"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 2019 年 3 月 27 日
I am not sure how to use diff to detect eta changes and plot appropriately. Any chance you could show me what you mean?

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2019 年 3 月 27 日
編集済み: Stephen23 2019 年 3 月 27 日
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 件のコメント
Benjamin
Benjamin 2019 年 3 月 28 日
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?
Stephen23
Stephen23 2019 年 3 月 28 日
編集済み: Stephen23 2019 年 3 月 28 日
"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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by