Expected a scalar. Non-scalars are not supported in IF or WHILE statements...?

5 ビュー (過去 30 日間)
Chaudhry Jibran Javaid
Chaudhry Jibran Javaid 2017 年 7 月 6 日
コメント済み: Adam 2017 年 7 月 7 日
I'm using the output of the following function block as an input to State Flow Chart
function pv =pv_output(array_size, SF_PV)
%load SF_PV in work space
Apanel=0.64; %effective area of solar panel
a_npv=0.1085; %solar panel efficency
Irr_array_size = length(SF_PV)/30;
A_PV=array_size*Apanel; %total effective area
k=1;
Modified_Irr = zeros(1,Irr_array_size);
for i=1:array_size
Modified_Irr(k) = SF_PV(i);
k=k+1;
i=i+30;
end
pv=(array_size*Modified_Irr*A_PV*a_npv);
and it results in following error, Expected a scalar. Non-scalars are not supported in IF or WHILE statements, or with logical operators. Instead, use ALL to convert matrix logicals to their scalar equivalents.
Transition '[pv==0...&&in>0]' "pv==0"
I would really appreciate if anyone can help me with this. Thanks
  2 件のコメント
Rik
Rik 2017 年 7 月 6 日
Try to make this an MWE (minimal working example), so people can try to replicate your problem.
I don't see any inherent problems at a glance, so I would advice you to copy and paste the entire error message (all red text). You can also use the debugging tools to find out what your code is doing exactly at each step.
Using the debugger in the step by step mode, you may also notice that i=i+30; does nothing in a for-loop, but might be used in a while loop. If you want steps of 30, use for i=1:30:array_size.
Also note that the final multiplication may yield unexpected results or errors if there is more than 1 non-scalar. Use .* for element-wise multiplication.
Adam
Adam 2017 年 7 月 7 日
I always like acronyms that are so obscure they have to also be explained in full everywhere they are used :D

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

回答 (2 件)

Jan
Jan 2017 年 7 月 6 日
編集済み: Jan 2017 年 7 月 6 日
Please post the complete error message. Currently it does not contain the information which line causes the problem.
Note that the code will not run as expected:
for i = 1:array_size
% Is "array_size" a scalar or a vector?
...
i=i+30; % Useless! i is incremented by the FOR loop
end
The "i=i+30" will be ignored, because the FOR loop increases i in steps of 1. Use this instead:
for i = 1:30:array_size
Or what about tis without any loop:
Modified_Irr = SF_PV(1:30:end);

Chaudhry Jibran Javaid
Chaudhry Jibran Javaid 2017 年 7 月 7 日
Rik Wisselink, Jan Simon. Thanks for your response, the code is running fine, I have plotted graph with this code to confirm. But the problem arises when I use output from this function for Stateflow chart. the part i=i+30 is also giving the desired solution as it cuts down the size of a matrix from 8760x1 to 292x1. I've attached the screen shot of that part of flowchart which the error refers to.
  1 件のコメント
Rik
Rik 2017 年 7 月 7 日
I am not familiar with this specific module, but I can tell you the line in your code does nothing. If the size of your matrix is cut down, there must be a similar line of code to what Jan suggested somewhere else.
At the very least post the entire error message. As is sometimes mentioned, the community either spends hours to help you or seconds to ignore you. It is on you to increase the likelihood of it being the former.

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

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by