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

1 view (last 30 days)
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

Walter Roberson
Walter Roberson on 30 Nov 2011
(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)

Walter Roberson
Walter Roberson on 29 Nov 2011
What exactly is the size of Data_Out ?
  4 Comments
Robbe
Robbe on 29 Nov 2011
I tried that (I basically displayed every variable one by one :) )
And the result is:
Data_Out =
1
Robbe
Robbe on 29 Nov 2011
... and line 71 is:
tout=sim(modelfile,duration,simoptions);
(it's from another .m-file; see my other post)

Sign in to comment.


the cyclist
the cyclist on 29 Nov 2011
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.

Robbe
Robbe on 29 Nov 2011
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
Walter Roberson
Walter Roberson on 30 Nov 2011
Sounds like you have a "clear all" in one of your scripts. "clear all" includes removing breakpoints.
Robbe
Robbe on 30 Nov 2011
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.


Robbe
Robbe on 30 Nov 2011
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
the cyclist
the cyclist on 30 Nov 2011
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.
Robbe
Robbe on 30 Nov 2011
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.

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!