"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

18 Ansichten (letzte 30 Tage)
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(~)
coder.extrinsic('xlsread')
current =3;
Signal=xlsread('test.xlsx'); % read from file
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

Antworten (1)

Les Beckham
Les Beckham am 10 Mai 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.

Community Treasure Hunt

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

Start Hunting!

Translated by