I have the following input file 'Test_14-Dec-2018.xls' attached
This is my code:
clc;
clear;
dataset = xlsread('Test_14-Dec-2018');
A = dataset(:,1:8);
B = A(A(:,1)==10,:); %for a=10s
k=1;
C=cell(1,4);
for p = 1:4
j=p*9;
i=j-8;
C{k} = B(i:j,:);
d=cell2mat(C);
x = d(:,2);
k=k+1;
end
Basically what the code does is extract that data into the matlab workspace and selects the data only when the first column has '10' data.
In the for loop, every 9 rows are selected and assigned a cell so that I have 9x8 in each 4 cells.
Cell2mat converts those cells into one matrix of 9x32. I am able to extract the second column from that.
Now, I want the data from every 8th column i.e. d(:,8), d(:,16), d(:,24), d(:,32) in a for loop but I am unable to do that.
After that I should be able to plot 'x' with each of those data from each column.
Any kind of help will be appreciated.

10 件のコメント

Bob Thompson
Bob Thompson 2018 年 12 月 17 日
What do you mean by, "I am unable to do that?" Have you run into some kind of error? Do you not know the commands necessary? Or do you not know the exact formulation for such a task?
Yoham Parelkar
Yoham Parelkar 2018 年 12 月 17 日
I tried with using another for loop:
for l =1:32
y=d(:,l);
l=l+8;
end
Doing this, only d(:,32) gets saved in the workspace. I am not able to store the data from d(:,8), d(:,16), d(:,24)
Bob Thompson
Bob Thompson 2018 年 12 月 17 日
You're unable to save more than d(:,32) because you're overwriting y each time you run the loop. If you want to save each result from a loop you should index your result. A better way of doing this would be the following.
y = d(:,[8,16,24,32]);
Yoham Parelkar
Yoham Parelkar 2018 年 12 月 17 日
See that's the thing,
I have mentioned till 32 since that's according to the input file attached
In future, I wouldn't know the exact dimensions of the dataset I would get in the workspace with any input file and that is why I am trying for a 'for' loop so that I don't have to alter my script each time for a different input file.
Thanks for replying so promptly though!
Bob Thompson
Bob Thompson 2018 年 12 月 17 日
編集済み: Bob Thompson 2018 年 12 月 18 日
for i = 1:floor(size(d,2)/8);
y(:,i) = d(:,i*8);
end
Try that.
Yoham Parelkar
Yoham Parelkar 2018 年 12 月 17 日
Again, it saves only the last column values i.e. column 32
Image Analyst
Image Analyst 2018 年 12 月 18 日
Bob, again, please, answers down below in the answers section, not up here in the comments section.
Bob Thompson
Bob Thompson 2018 年 12 月 18 日
Ha, you are correct, I had an error. It has been fixed now.
Yoham Parelkar
Yoham Parelkar 2018 年 12 月 18 日
Thanks Bob for fixing the error.
Now I want to plot multiple values of 'y' with 'x'.
plot(x,y(:,m))
Adding the above line only gives me comparison of x with obly one value of 'y'. How should I edit the code in such a way that all values of 'y' gets plotted with 'x'?
Bob Thompson
Bob Thompson 2018 年 12 月 18 日
You need to set 'hold on'. That will allow you to plot multiple lines on the same figure. You can do this before the loop.

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

 採用された回答

Yoham Parelkar
Yoham Parelkar 2018 年 12 月 18 日

0 投票

Thank you Bob and Madhan Ravi for your help and inputs!
I was able to successfuly plot and further add new commands and complete the script.
This is correct code:
clc;
clear;
dataset = xlsread('Test_14-Dec-2018');
A = dataset(:,1:8);
B = A(A(:,1)==10,:); %for a=10s
k=1;
C=cell(1,4);
for p = 1:4
j=p*9;
i=j-8;
C{k} = B(i:j,:);
d=cell2mat(C);
x = d(:,2);
k=k+1;
end
for m = 1:floor(size(d,2)/8);
y(:,m) = d(:,m*8);
hold on
plot(x,y(:,m))
end

その他の回答 (1 件)

madhan ravi
madhan ravi 2018 年 12 月 17 日
編集済み: madhan ravi 2018 年 12 月 17 日

0 投票

Replying to your latter comment :
y=cell(1,numel(1:8:32));
for l=1:8:32
y{l}=d(:,l);
end
celldisp(y) % [y{:}] to convert it to double from cell
y=d(:,[1:8:32])

3 件のコメント

Yoham Parelkar
Yoham Parelkar 2018 年 12 月 17 日
Thank for the reply!
Although I mentioned it wrong in the first place, I altered your code to:
y=cell(1,numel(8:8:32));
for l=8:8:32
y{l}=d(:,l);
end
celldisp(y) % [y{:}] to convert it to double from cell
y=d(:,[8:8:32])
I will try to complete the script and will revert to you.
madhan ravi
madhan ravi 2018 年 12 月 17 日
Anytime :) , if my code worked make sure to accept the answer else let know what needs to be done after you analyse(take your time) let me take a short nap by then ;-).
Yoham Parelkar
Yoham Parelkar 2018 年 12 月 18 日
Well actually, your code only extracts every 8th column and saves those 4 columns into a new variable which is nothing but the same as of 'd' variable.
I don't want to make a new dataset of those values but only extract those columns and plot it with 'x'

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by