store multiple outputs into one matrix

25 ビュー (過去 30 日間)
C.G.
C.G. 2021 年 10 月 22 日
編集済み: Chris 2021 年 10 月 22 日
My code currently produces 14 individual variables (x1-7 and y1-7), I am currently plotting these on a graph using hold on statements.
Is there a way to save these into 2 7x1 doubles, one for x variables and one for y variables so I can just plot these against each other?
file = dir('ATF_0.csv'); %read the files into matlab
num_files = length(file); %record how many files have been found
T = table2array(readtable(file.name)); %read in the values
T = T(T(:,6)<=-0.07,:);
%s1
S1 = T(:,5) >= 0.14 & T(:,5) <= 0.149;
TS1 = T(S1, :);
[y1, idx1] = max(TS1(:, 6));
x1 = TS1(idx1, 5);
%s2
S2 = T(:,5)>= 0.13 & T(:,5) <= 0.139;
TS2 = T(S2, :);
[y2, idx2] = max(TS2(:, 6));
x2 = TS2(idx2, 5);
%s3
S3 = T(:,5)>= 0.121 & T(:,5) <= 0.129;
TS3 = T(S3, :);
[y3, idx3] = max(TS3(:, 6));
x3 = TS3(idx3, 5);
%s4
S4 = T(:,5)>= 0.11 & T(:,5) <= 0.12
TS4 = T(S4, :);
[y4, idx4] = max(TS4(:, 6));
x4 = T(idx4,5);
%s5
S5 = T(:,5)>= 0.1 & T(:,5) <= 0.109
TS5 = T(S5, :);
[y5, idx5] = max(TS5(:, 6));
x5 = TS5(idx5, 5);
%s6
S6 = T(:,5)>= 0.09 & T(:,5) <= 0.099
TS6 = T(S6, :);
[y6, idx6] = max(TS6(:, 6));
x6 = TS6(idx6, 5);
%s7
S7 = T(:,5)>= 0.08 & T(:,5) <= 0.089
TS7 = T(S7, :);
[y7, idx7] = max(TS7(:, 6));
x7 = TS7 (idx7, 5);

回答 (1 件)

Chris
Chris 2021 年 10 月 22 日
Arrays with compatible dimensions can be concatenated using either square brackets or the cat() function.
In this case, it looks like your variables are all 1x1 arrays, so:
x = [x1; x2; x3; x4; x5; x6; x7];
would likely be sufficient to create a 7x1.
For a row vector instead of a column vector, use commas instead of semicolons.
  2 件のコメント
C.G.
C.G. 2021 年 10 月 22 日
ok thank you, that makes sense.
I want to put the above code in a for loop where I am doing the steps for 2 different files and therefore generating the 7 variables for each file, would it be possible to use that to then put the next set of outputs in the next column?
Chris
Chris 2021 年 10 月 22 日
編集済み: Chris 2021 年 10 月 22 日
Yes.
You could initialize x as an empty array:
x = []
and inside the for loop, concatenate it to itself.
x = [x; new_x];
another method:
x(end+1) = new_x;
Similarly for y.
I'm not sure whether this entirely answers your question. If you're trying to get x and y columns in the same array, that's also possible:
xy = [];
for ...
xy = [xy; new_x, new_y];

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by