Info

此问题已关闭。 请重新打开它进行编辑或回答。

Why is my Index exceeds matrix dimensions error gone after i add an pause to my code?

1 次查看(过去 30 天)
Hello everyone,
The following is a function of my entire code that is desined to plot several circles on an axis inside the interface "GUI" i am designing. I added the variables that normaly are defined by different functions so the code can be run.
I get the error Index exceeds matrix dimension when running this function but when i add an pause or a big while loop and do exactly the same, the error Index exceeds matrix dimension dos NOT appear and if i remove the pause/whileloop after the code has run one time the error doesnt apear anymore until i restarted matlab and i can't figure out why this happens. I added comments inside the code where the error apears.
%%Get the handles and disable plot and save
%Get the structure with handles and user data.
%handles = guidata(gcbo);
%set(handles.PlotTag, 'Enable', 'of')
%set(handles.SaveCalculationFileTag,'Enable', 'of')
%%Define the variables
%Define the center on the first plot for the x axis
Xcenter1 = 0;
%Define the center on the second plot for the x axis
Xcenter2 = 0;
%Define the center on the first plot for the y axis
Ycenter1.RadiusZero = 8;
%Define the center on the second plot for the y axis
Ycenter2.RadiusZero = 0;
%Define the x vector for the plot
x=0:0.01:1;
%Define the y vector for the plot
y=0:0.01:1;
%Set the hold on axis of so any old plot will be overwritten
hold off
%%Test if the table data is correct
%Test if the data the user put into the table is bigger than zero
%value = get(handles.TableWals2,'data');
%I create a standart table here normaly this value is called with:
% get(handles.TableWals2,'data')
value = {rand(3,3)};
for ii = 1:3
value(ii,1) = {ii};
value(ii,2) = {1};
value(ii,3) = {0.5};
value(ii,4) = {0};
value(ii,5) = {1};
value(ii,6) = {1};
value(ii,7) = {0};
value(ii,8) = {0};
end
LoopValue = size(value);
for ii = 1 :LoopValue(1)
for jj = 1 :LoopValue(2)
if (jj ==3)
jj = 4;
end
ErrorTest = cell2mat(value(ii,jj));
%If the data is negative open an error box
if (ErrorTest<0)
errordlg('The input value has to be greater than zero', 'Input Value Error')
ErrorHandles.Error(9) = 1;
setappdata(0,'ErrorHandles',ErrorHandles)
break
end
if (isnan(cell2mat(value(ii,jj))))
errordlg('The input value has to be a number', 'Input Value Error')
ErrorHandles.Error(10) = 1;
setappdata(0,'ErrorHandles',ErrorHandles)
valueNaN = 0;
break
else
valueNaN = 1;
end
end
%If the data is enterd correctly continue with the function. Else exit.
%
if (ErrorTest<0)||(valueNaN == 0)
TableDataisCorrect = 0;
break
else
TableDataisCorrect = 1;
end
end
for ii = 1:LoopValue(1)
%I create a standart table here normaly this value is called with:
% get(handles.TableWals2,'data')
value = {rand(3,3)};
for ii = 1:3
value(ii,1) = {ii};
value(ii,2) = {1};
value(ii,3) = {0.5};
value(ii,4) = {0};
value(ii,5) = {1};
value(ii,6) = {1};
value(ii,7) = {0};
value(ii,8) = {0};
end
ErrorTest = cell2mat(value(ii,3));
if (isnan(cell2mat(value(ii,3))))
errordlg('The input value has to be a number', 'Input Value Error')
ErrorHandles.Error(11) = 1;
setappdata(0,'ErrorHandles',ErrorHandles)
TableDataisCorrect = 0;
break
end
%If the data is not between -1 or 0.5
if (ErrorTest<-1)||(ErrorTest>0.5)
errordlg('Input error: enter a number between [-1]and [0.5]', 'Input Value Error')
ErrorHandles.Error(12) = 1;
setappdata(0,'ErrorHandles',ErrorHandles)
TableDataisCorrect = 0;
break
end
end
%%Start the function after testing is complete
%If the data is enterd correctly continue with the function. Else exit.
if (TableDataisCorrect == 1)
ErrorHandles.Error(9) = 0;
ErrorHandles.Error(10) = 0;
ErrorHandles.Error(11) = 0;
ErrorHandles.Error(12) = 0;
setappdata(0,'ErrorHandles',ErrorHandles)
%%%%%%%%%%%%%%%%%%%
%Get user thickness
%%%%%%%%%%%%%%%%%%%
Get_Thicknes = value;
%%Get the user input from table2
%Get the user input for the number of layers and fill the user thickness
%into radius start with the last layer and work from inside to outside
zz = size(value);
zz = zz(1);
ii = 1;
%iiMax = (str2double((get(handles.Wals2Tag,'string'))));
iiMax = 3;
while (zz >0 && ii<iiMax+1)
Radius.RadiusLayer(ii) = Get_Thicknes(zz,2);
ii = (ii + 1);
zz = (zz - 1);
end
%%Get the user input from roller2
%Get the user input for the number of layers he has. Change it to a
%double and go into a for loop.
%handles.num = str2double(get(handles.Wals2Tag,'string'));
handles.num = 3;
if (handles.num == 0)
Core = 30;
Save.RadiusLayer2(1) = Core;
MaxRadius2 = Core;
YcenterAll2 = Ycenter2.RadiusZero + MaxRadius2;
plot(Xcenter2+Save.RadiusLayer2(ii)*cos(2*pi*x),YcenterAll2+Save.RadiusLayer2(ii)*sin(2*pi*y));%the circle plot
hold on
else
%%Calculate all radii and the centerpoint.
for ii = 1 : handles.num
%Core = str2double(get(handles.Core2Tag,'string'));
Core = 30;
Save.RadiusLayer2(1) = Core;
%Calculate all radius and save them for each layer
Save.RadiusLayer2(ii+1) = Save.RadiusLayer2(ii)+cell2mat(Radius.RadiusLayer(ii));
%Define the biggest radius, this is needed for the plot
RadiusMax2.Max = Save.RadiusLayer2(end);
%Calculate the new Ycenterpoint of the biggest radius, this is
%needed for the plot
MaxRadius2 = (RadiusMax2.Max);
YcenterAll2 = Ycenter2.RadiusZero + MaxRadius2;
end
%%Plot the users layers
%Plot the user thickness in circles
for ii = 1 : (handles.num+1)
plot(Xcenter2+Save.RadiusLayer2(ii)*cos(2*pi*x),YcenterAll2+Save.RadiusLayer2(ii)*sin(2*pi*y));
%Put hold on to save al data that you are plotting
hold on
end
end
%%Plot the core
%Plot a circle over the core and make it black
hMyCircle = rectangle('position',[(Xcenter2-Core) (YcenterAll2-Core) (2*Core) (2*Core)],'curvature',[1,1]);
set(hMyCircle,'FaceColor','black')
%%Save the Radius and update the guidata
%Save MaxRadius2 into handles
handles.MaxRadius = MaxRadius2;
%%Get the user input from table1
%Get the user thickness
%I create a standart table here normaly this value is called with:
% get(handles.TableWals1,'data')
value2 = {rand(3,3)};
for ii = 1:3
value2(ii,1) = {ii};
value2(ii,2) = {1};
value2(ii,3) = {0.5};
value2(ii,4) = {0};
value2(ii,5) = {1};
value2(ii,6) = {1};
value2(ii,7) = {0};
value2(ii,8) = {0};
end
%Get_Thicknes = get(handles.TableWals1,'data');
Get_Thicknes = value2;
zz = size(value2);
zz = zz(1);
%zz = str2double((get(handles.Wals1Tag,'string')));
%zoalng zz groter is als 0..
ii = 1;
%iiMax = (str2double((get(handles.Wals1Tag,'string'))));
iiMax = 3;
while (zz >0 && ii<iiMax+1)
Radius.RadiusLayer(ii) = Get_Thicknes(zz,2);
ii = (ii + 1);
zz = (zz - 1);
end
end
%%Get the user input from Roller1
%Get the user input for the number of layers he has. Change it to a
%double and go into a for loop.
%handles.num = str2double(get(handles.Wals1Tag, 'string'));
handles.num = 3;
if (handles.num == 0)
Core =20;
Save.RadiusLayer1(1) = Core;
MaxRadius1 = Core;
YcenterAll1 = 2*MaxRadius2 + MaxRadius1;
ii = 1;
plot(Xcenter1+Save.RadiusLayer1(ii)*cos(2*pi*x),YcenterAll1+Save.RadiusLayer1(ii)*sin(2*pi*y));%the circle plot
hold on
else
%%Calculate all radii and the centerpoint.
for ii = 1 : handles.num
%In this part of the function i usualy get the error Index exceeds
%matrix dimension when "handles.num = 0" and according to the ifstatement
%it is imposible that this forloop of the statement is entered.
%But when i add an pause or a big while loop and
%do exactly the same, the error Index exceeds
%matrix dimension dos NOT appear and i can't figure
%out why.
% testingError = 1;
% while(testingError ~=3000)
% testingError = testingError+1;
% end
Core = 20;
Save.RadiusLayer1(1) = Core;
%Calculate all radius and save them for each layer
Save.RadiusLayer1(ii+1) = Save.RadiusLayer1(ii)+cell2mat(Radius.RadiusLayer(ii));
%Define the biggest radius, this is needed for the plot
RadiusMax1.Max = Save.RadiusLayer1(end);
%Calculate the new Ycenterpoint of the biggest radius, this is
%needed for the plot
MaxRadius1 = RadiusMax1.Max;
Ycenter1.Max = Ycenter1.RadiusZero + MaxRadius1;
end
%calculate the new center point on the Y axis
MaxRadius1 = (RadiusMax1.Max);
YcenterAll1 = 2*MaxRadius2 + MaxRadius1;
%%Plot the users layers
%Plot the user thickness in circles
for ii = 1 : (handles.num+1)
plot(Xcenter1+Save.RadiusLayer1(ii)*cos(2*pi*x),YcenterAll1+Save.RadiusLayer1(ii)*sin(2*pi*y));
end
end
%%Plot the core
%Plot a circle over the core and make it black
hMyCircle = rectangle('position',[(Xcenter1-Core) (YcenterAll1-Core) (2*Core) (2*Core)],'curvature',[1,1]);
set(hMyCircle,'FaceColor','black')
axis equal
%%Test if the table data is correct.
%handles.TableWals2Done2 = 0;
handles.SaveRadius1 = MaxRadius1;
handles.SaveRadius2 = MaxRadius2;
%Update the handles structure
setappdata(0, 'test',handles)
% End function TableWals2_CellEditCallback
The things i have looked at are:
  • hyperthreading
  • Looping between functions
  • Double defined variables
  • I also tryed adding a whileloop to the function the effect that this had was that the error Index exceeds matrix dimensions appeared only one out of ten times.
But none of these options fixed the problem so im hoping that someone has had similar problems and find out how to fix it.
The variables that are changed that make this function crash:
  • handles.num
  • Core
Im using matlab version R2011b on an Fujitsu i5 core with windows 7 operating system.
Regards,
Nick
  3 个评论
Nick
Nick 2013-1-18
I will do my best on the tags this is the first post i ever made.
The error happens in the forloop that starts in line 233.
I added the while loop at line 242 and i added the while loop as an alternative for the pause to slow the function down
And hyperthreading "Hyper-Threading technology" is when multiple tasks are running at once I thought matlab was running multiple functions at the same time. I tryed disabling it using the function: maxNumCompThreads(1). And i also tryed the other things that are mentioned in: http://www.mathworks.com/matlabcentral/answers/1848-turn-off-hyperthreading-when-i-am-using-matlab
To explain (double defined variables) my code has over 60 functions and more than 40 handles that are used at different places. I have looked if i used the handles that i am using in this code were overwritten in a different function. Or by matlab.
And "Looping between functions" several of the functions i made are called by different functions for updates on the handles or variabels. I looked here if somewere the functions enterd a endles loop that caused the error.
Jan
Jan 2013-1-18
@Nick: Thanks for the clarifications. I really appreciate this.
"Hyper-threading" means, that the operating system can address two logical cores for one physical core. While hyper-threading can be triggered in the BIOS/EFI-setup only, "multi-threading" can be controlled for each program, e.g. by the mentioned maxNumCompThread.

回答(1 个)

Jan
Jan 2013-1-18
编辑:Jan 2013-1-18
Just a note: Replace:
cell2mat(value(ii,3))
by the nicer and faster:
value{ii,3}
But to your problem: A PAUSE statement starts the evaluation of callbacks. Does the problem appear inside a callback, and if so, what are the Interruptible and BusyAction properties of the calling GUI object?
  1 个评论
Nick
Nick 2013-1-18
编辑:Nick 2013-1-21
The problem appears after i called the handle from an edit box. In line 223 this is commented out: handles.num = str2double(get(handles.Wals1Tag,'string')); The value of Wals1Tag is in average between 0 and 10. But somthimes when the value is '0' the error occures.
The Interruptible value = on; and The BusyAction = queue;

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by