# "Colon operands must be all the same type, or mixed with real scalar doubles" error I'm trying to run excel file into Matlab function

35 views (last 30 days)
Mhna saif on 10 May 2020
Answered: Les Beckham on 10 May 2020
Hello there! I'm trying to run excel file using Matlab function but this error keep appears to me "Colon operands must be all the same type, or mixed with real scalar doubles".Can someone help me to fix it !!
This is my Excel file
test.xlxs By using this code
function Voltage = fcn(~)
current =3;
frq= size(Signal,1);
S12= size(Signal,2);
for i=1 :Signal %%%%% Colon operands must be all the same type, or mixed with real scalar doubles
if (frq(i) == 2.4500 )
x=S12(i);
end
end
Power= x^2;
Voltage = Power/current;
end

Les Beckham on 10 May 2020
Well there are several things wrong with this code.
1. You can't use size() to extract things from an array. Replace frq= size(Signal,1); with frq = Signal(:,1);
2. You can't use an array as an index (this is the cause of the error message since Signal will be an array after reading the data from your spreadsheet). Use, for example, for i = 1:numel(frq).
Also, though not wrong, there is an easier way to find your 'x' without using a loop: x = S12(frq==2.45)
If you are planning to use this in a Simulink Matlab function block (I'm just guessing based on the coder.extrinsic call), I would recommend against repeatedly calling xlsread in a Simulink simulation unless, for some strange reason, you expect text.xlsx to be changing during the simulation run.
There is probably a better way to achieve what you are trying to do. If you explain what you are trying to do you may get some suggestions on how to do it more effectively.