Exceeds array bounds Error when running m-file, but no problem if the function is run alone.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Luk H.
am 9 Sep. 2019
Kommentiert: Stephen23
am 10 Sep. 2019
Hello, I m new at Matlab and have a simple question:
I wrote a short m-file function with the purpose of showing me the exact index where 3 conditions are fulfilled.
function[II]=cutdataB(VP,VT,PH)
global switchdata
II=find(switchdata(:,1)==VP&switchdata(:,3)==VT&switchdata(:,4)==PH-1)
If I run it, Matlab says:
cutdata (2,2,2)
Index in position 2 exceeds array bounds.
Error in cutdataB (line 3)
The confusing part is coming now:
Entering the third line alone, with filling in VP=2, VT=2, PH=2, results in Matlab showing the correct index vector
II=find(switchdata(:,1)==2&switchdata(:,3)==2&switchdata(:,4)==2-1)
II =
33
So, what is wrong with my m-file?
(If you re wondering, actually it's just a simplified version of another m file that I'm going to write. But in order to get that running, I m trying to at least manage to create an easy one)
Best regards
1 Kommentar
Stephen23
am 10 Sep. 2019
"So, what is wrong with my m-file?"
Global variables.
etc.
Use one of the more reliable methods described in the MATLAB documentation:
Personally I find nested functions very intuitive and reliable.
Akzeptierte Antwort
Ted Shultz
am 9 Sep. 2019
I suspect your problem has to do with using different matrix named switchdata. Many people feel that is best to avoid using global variables if possible to avoid some of these types of confusion. Would it be possible to have the switchdata matrix passed to this function, rather than access it as a global?
1 Kommentar
Stephen23
am 10 Sep. 2019
"Many people feel that is best to avoid using global variables..."
Including the MATLAB documentation, which clearly states: "Use global variables sparingly, if at all" and recommends better ways of passing data between workspaces.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!