In an assignment A(I) = B, the number of elements in B and I must be the same.

Hi, I'm getting following error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
... even though the number of elements are the same. I displayed their values and their number of elements to check this.
This is my code:
CS = 1;
STROOM = zeros(1,10);
STROOMB = zeros(1,10);
Counter = 0;
switch state
case STATE.READ % present state is READ
CS=0;
if (Counter ~= 10);
STROOMB(Counter+1) = Data_Out;
Counter = Counter+1;
next_state = STATE.READ;
This is the output:
(3) RUNNING TEST test
1
1
??? Error using ==> Model_run at 71
Error in 'Model_ADConverter/FSM_template/S-Function' while executing MATLAB S-function 'FSM_ADConverter', flag = 3 (output), at
time 0.0.
Caused by:
Error using ==> Model_run at 71
In an assignment A(I) = B, the number of elements in B and
I must be the same.
I've been looking hours for a solution (I'm pretty new to Matlab) but can't find it... Does anyone know the answer?
Thanks in advance!

 Accepted Answer

(To exhume the key point towards solving this:)
If you have "clear all" in one of your functions or scripts, comment that out or remove it, as one of the effects of it is to remove all breakpoints and "dbstop" commands.
Once those "clear all" commands are disabled, you can use
dbstop if error
at the MATLAB command line and then run, in order to determine which line is causing the problem.

More Answers (4)

What exactly is the size of Data_Out ?

4 Comments

Data_Out is originally a matrix of the form [time1 value1; time2 value2;...;timen valuen] but in the code the values are given one by one.
I assume line 71 is "STROOMB(Counter+1) = Data_Out;"
You could double check that Data_Out is 1x1 by printing it one line before, just a line with "Data_Out" and no suppression.
I tried that (I basically displayed every variable one by one :) )
And the result is:
Data_Out =
1
... and line 71 is:
tout=sim(modelfile,duration,simoptions);
(it's from another .m-file; see my other post)

Sign in to comment.

Although you have shown us some code, it is too incomplete for us to run and try to reproduce the error. Can you post the smallest possible chunk of code that we would actually be able to run, to reproduce your error?
Are you familiar with breakpoints? You could try adding a breakpoint at the line that errors out, and look at the sizes.
Also, you could use a "try ... catch" structure around the line that gives the error, stopping the code in that way.
I can't use the debugger cause the function-file is run by another file and so Matlab just says the error is on the line where the second function is called
This is the code in the function file:
function [Output,next_state]=updateFSM(state,Input)
% (2a) Initialize input variables, for readability:
% Give the different inputs a name, in the order which they are muxed
Data_Out = Input;
% (2b) The FSM
% default transition
next_state=state;
% default outputs
CS = 1;
STROOM = zeros(1,10);
STROOMB = zeros(1,10);
Counter = 0;
% define next state and outputs based on the present state and inputs
switch state
case STATE.READ % present state is READ
CS=0;
if (Counter ~= 10)
disp(numel(Data_Out));disp(numel(Counter+1));
% disp(Data);disp(size(Counter+1));disp(size(STROOMB(Counter+1)));disp(size(Data));
try STROOMB(Counter+1) = Data_Out; catch
Counter = Counter+1;
next_state = STATE.READ; %declare state transition
else
Counter = 0;
next_state = STATE.CALC;
end
case STATE.CALC % present state is CALC
CS = 1;
if (Counter ~= CALCTIMER)
Counter = Counter+1;
next_state = STATE.CALC; %declare state transition
else
STROOM = STROOMB;
Counter = 0;
next_state = STATE.READ;
end
end %switch state
% (2c) put out the outputs in the order which they are demuxed
Output(1) = CS;
Output(2) = STROOM;
end %function updateFSM
end %main function
and this is what's put in from the second file (the one that's run):
switch test_scenario
case 'test'
Data_Out = [0 1;
5 0;
10 1;
15 0];
end
% (2) open model
disp(['(2) OPENING MODEL ' modelfile ])
open(modelfile)
% (3) run simulation
disp(['(3) RUNNING TEST ' test_scenario])
simoptions = simset(simget,'Solver','FixedStepDiscrete','FixedStep',1);
tout=sim(modelfile,duration,simoptions);
The error says the problem is situated on this last line... Thanks for your answer!

10 Comments

You appear to have a try / catch / else / end structure, but "else" is not a defined control keyword for try / catch / end blocks.
I removed it already
Just added it after you suggested it but after receiving the error you just described I removed it again
I think it was the cyclist that suggested adding try/catch .
At the MATLAB prompt, give the command
dbstop in updateFSM
and then run again. It should stop in the debugger at the beginning of the routine.
dbstop in updateFSM
??? Error using ==> dbstop
Cannot find function "updateFSM".
Must be a subfunction then. In that case, try clicking a breakpoint in to existence in updateFSM. Breakpoints do work when a script is called from another script.
I tried that but my second script seems to remove the breakpoint...
Actually the second script opens a Simulink model in which an S-function in used. The error occurs in this s-function.
Sounds like you have a "clear all" in one of your scripts. "clear all" includes removing breakpoints.
that was true!
I removed it temporary, and the debugger goes straight through the piece of code I gave you guys (explains why I've been looking for hours after something that seems to be right) and stops at another A(I)=B piece :s
I thought the one I gave you was the only one of this type...
Thanks for your help!

Sign in to comment.

So back to the original problem: further on in the code I want to give a matrix as output of the s-function There should be two outputs: the first one being a constant, the second one a matrix Is this possible?

2 Comments

I suggest that you start a new thread with this question, rather than burying it here. Also, consider "accepting" the answer here that you received that, if you found one critically helpful.
I can't really accept my own answer (the answer to the question was to remove the 'clear all' which is suggested in one of my own replies)
Thanks for your help!

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!