issue when using 'for' and 'elseif'

hi, im very new to matlab and i'm trying to write code which sorts through a table and finds values which adhere to the conditions ie waveheight is 2<x<4 or 4<x<6 etc.
the code is below, my issue is whenever i try write the count-
"for T2 =1:size(T2)"
it just gives me an output of 1, I have worked out the 'elseif' segment of this code but it wont work unless I get this for loop to work. I've tried loads of different variations of code but i just cant seem to get it to work
I am also on mac
thanks, ben.

4 件のコメント

Geoff Hayes
Geoff Hayes 2020 年 4 月 24 日
Ben - how does the attached code refer to your question? I don't see any for loops or if/elseif segments. As for the code
for T2 =1:size(T2)
what is T2? Presumably it is an array...in which case you don't want to re-use this variable as the loop iterator. Also, are you iterating over the rows or columns (or some other dimension) of T2? Calling size returns an array of the dimensions sizes. So the size of a 2x3 array would be [2 3]. Your code could look like
for k = 1:length(T2)
where we assume that T2 is a 1-D array. If you want to iterator over the number of rows, you would do
for k = 1:size(T2,1)
or columns
for k = 1:length(T2,2)
etc.
Ben Murphy
Ben Murphy 2020 年 4 月 25 日
Okay yeah that seems quite obvious now looking at it, my aim with the else if bit is to set up a count which counts the number of times the value is <2, then 2<x<4 etc. But I can’t figure out how to do it so got rid of the code, I suppose my question is how can I get matlab to count up the number of values <2 in my table using ‘if,else if’ as I just can’t work out what to do, thanks.
Geoff Hayes
Geoff Hayes 2020 年 4 月 25 日
Ok - I recommend reposting the code for this problem, where you include your for loop and if/else checks. You may not even need a for loop..
Ben Murphy
Ben Murphy 2020 年 4 月 25 日
okay I've attached the code, im guessing I need some sort of empty array for the loop to put the values in, and a way of counting it, but my teacher has specified it has to be done using 'for' and' if/else' which is why i havent approached a different method.

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

 採用された回答

Geoff Hayes
Geoff Hayes 2020 年 4 月 25 日

0 投票

Ben - from your code,
a=readtable('data-all .xlsx','range','G2:G500');
a.Properties.VariableNames = {'waveheight'};
it looks like you have a single column which you have named waveheight. To get a column of this data, you would then do
waveheightData = a.waveheight;
and iterate over it like
for k = 1:length(waveheightData)
if waveheightData(k) > 2 && waveheightData(k) < 4
% do something
elseif waveheightData(k) > 4 && waveheightData(k) < 6
% do something
%etc.
end
end
So you would set up all the conditions necessary to group the data by intervals. You would probably want to create an array of elements, one for each interval, and then increment that element in the % do something part of the code.

1 件のコメント

Ben Murphy
Ben Murphy 2020 年 4 月 25 日
its finally worked! Many thanks the help.

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

その他の回答 (0 件)

カテゴリ

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

製品

質問済み:

2020 年 4 月 24 日

コメント済み:

2020 年 4 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by